mirror of
https://github.com/wasp-lang/wasp.git
synced 2024-12-24 09:34:28 +03:00
Extracted utility methods.
This commit is contained in:
parent
e3153dc233
commit
d4fd70fe7b
@ -1,7 +1,7 @@
|
||||
cc_library(
|
||||
name = "hello-world",
|
||||
srcs = ["hello-world.cpp"],
|
||||
hdrs = ["hello-world.hpp"],
|
||||
name = "utils",
|
||||
srcs = ["utils.cpp"],
|
||||
hdrs = ["utils.hpp"],
|
||||
visibility = [
|
||||
"//test:__pkg__"
|
||||
]
|
||||
@ -11,6 +11,6 @@ cc_binary(
|
||||
name = "stick-cli",
|
||||
srcs = ["cli.cpp"],
|
||||
deps = [
|
||||
":hello-world"
|
||||
":utils"
|
||||
]
|
||||
)
|
||||
|
@ -5,26 +5,9 @@
|
||||
#include <regex>
|
||||
#include <iterator>
|
||||
|
||||
#include "./hello-world.hpp" // TODO: redundant, remove.
|
||||
#include "utils.hpp"
|
||||
|
||||
bool endsWith(const std::string& str, const std::string& suffix) {
|
||||
return str.length() >= suffix.length() && 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix);
|
||||
}
|
||||
|
||||
std::string readFile(const std::string& filepath) {
|
||||
std::ifstream file(filepath);
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
return buffer.str();
|
||||
}
|
||||
|
||||
void writeFile(const char* filepath, const std::string& content) {
|
||||
std::ofstream file(filepath);
|
||||
if (file.is_open()) {
|
||||
file << content;
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
using namespace stic;
|
||||
|
||||
void printHelp() {
|
||||
std::cout << "Usage: stick-cli <your-spec-file>.wasp" << std::endl;
|
||||
@ -48,7 +31,7 @@ void buildWebAppFromWasp(std::string waspFileContent) {
|
||||
|
||||
std::string indexHtml = "<html> <head> <title>" + pageName + "</title> </head> <body> Welcome to my \"" + pageName + "\" page! </body> </html>";
|
||||
|
||||
writeFile("index.html", indexHtml);
|
||||
utils::writeFile("index.html", indexHtml);
|
||||
}
|
||||
|
||||
int main (int argc, char* argv[]) {
|
||||
@ -57,10 +40,10 @@ int main (int argc, char* argv[]) {
|
||||
if (argc != 2) { printHelp(); return 1; }
|
||||
// First parameter should be .wasp file path.
|
||||
const std::string waspFilepath(argv[1]);
|
||||
if (!endsWith(waspFilepath, ".wasp")) { printHelp(); return 1; }
|
||||
if (!utils::endsWith(waspFilepath, ".wasp")) { printHelp(); return 1; }
|
||||
|
||||
// Read wasp file.
|
||||
std::string waspFileContent = readFile(waspFilepath);
|
||||
std::string waspFileContent = utils::readFile(waspFilepath);
|
||||
|
||||
buildWebAppFromWasp(waspFileContent);
|
||||
|
||||
|
29
stic/src/utils.cpp
Normal file
29
stic/src/utils.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace stic {
|
||||
namespace utils {
|
||||
|
||||
bool endsWith(const std::string& str, const std::string& suffix) {
|
||||
return str.length() >= suffix.length() && 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix);
|
||||
}
|
||||
|
||||
std::string readFile(const std::string& filepath) {
|
||||
std::ifstream file(filepath);
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
return buffer.str();
|
||||
}
|
||||
|
||||
void writeFile(const char* filepath, const std::string& content) {
|
||||
std::ofstream file(filepath);
|
||||
if (file.is_open()) {
|
||||
file << content;
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
} // namespace stic
|
15
stic/src/utils.hpp
Normal file
15
stic/src/utils.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace stic {
|
||||
namespace utils {
|
||||
|
||||
bool endsWith(const std::string& str, const std::string& suffix);
|
||||
|
||||
std::string readFile(const std::string& filepath);
|
||||
|
||||
void writeFile(const char* filepath, const std::string& content);
|
||||
|
||||
} // namespace utils
|
||||
} // namespace stic
|
@ -17,12 +17,12 @@ cc_library(
|
||||
|
||||
# Individual tests (per target). Each expects .test.cpp file with same name to exist.
|
||||
# Add new tests here! Also, add them to test suite all-tests below.
|
||||
simple_target_test("hello-world")
|
||||
simple_target_test("utils")
|
||||
|
||||
# Test suite that runs all the tests.
|
||||
test_suite(
|
||||
name = "all-tests",
|
||||
tests = [
|
||||
"hello-world"
|
||||
"utils"
|
||||
]
|
||||
)
|
||||
|
@ -1,7 +0,0 @@
|
||||
#include "catch.hpp"
|
||||
|
||||
#include "hello-world.hpp"
|
||||
|
||||
TEST_CASE("Returns hello world.") {
|
||||
REQUIRE(sayHi() == "Hello world!");
|
||||
}
|
13
stic/test/utils.test.cpp
Normal file
13
stic/test/utils.test.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include "catch.hpp"
|
||||
|
||||
#include "utils.hpp"
|
||||
|
||||
using namespace stic;
|
||||
|
||||
TEST_CASE("endsWith() returns true for suffix") {
|
||||
REQUIRE(utils::endsWith("ABCD", "CD") == true);
|
||||
}
|
||||
|
||||
TEST_CASE("endsWith() returns false for non-suffix") {
|
||||
REQUIRE(utils::endsWith("ABCD", "C") == false);
|
||||
}
|
Loading…
Reference in New Issue
Block a user