From 142671c1775411545bdca25d568e1763f7df46f9 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sat, 8 Mar 2014 15:26:27 -0800 Subject: [PATCH] Separate spec helpers files for compiler and runtime specs --- .../build_tables/build_tables_spec.cpp | 2 +- spec/compiler/build_tables/first_set_spec.cpp | 2 +- .../build_tables/follow_sets_spec.cpp | 2 +- .../build_tables/item_set_closure_spec.cpp | 2 +- .../build_tables/rule_can_be_blank_spec.cpp | 2 +- .../build_tables/rule_transitions_spec.cpp | 2 +- spec/compiler/compile_examples.cpp | 12 ++++++--- spec/compiler/compiler_spec_helper.h | 18 +++++++++++++ spec/compiler/compiler_specs.cpp | 12 +++++++++ .../helpers/character_set_helpers.cpp} | 17 +++++------- spec/compiler/helpers/character_set_helpers.h | 14 ++++++++++ spec/{ => compiler}/helpers/equals_pointer.h | 0 spec/{ => compiler}/helpers/stream_methods.h | 0 spec/compiler/prepare_grammar_spec.cpp | 2 +- spec/compiler/rules/character_set_spec.cpp | 2 +- spec/compiler/rules/pattern_spec.cpp | 2 +- spec/compiler/rules/rules_spec.cpp | 2 +- spec/runtime/arithmetic_spec.cpp | 2 +- spec/runtime/json_spec.cpp | 2 +- spec/runtime/runtime_spec_helper.h | 13 +++++++++ spec/{main.cpp => runtime/runtime_specs.cpp} | 4 +-- spec/runtime/tree_spec.cpp | 20 +++++++------- spec/spec_helper.h | 27 ------------------- tree_sitter.gyp | 9 ++----- 24 files changed, 99 insertions(+), 71 deletions(-) create mode 100644 spec/compiler/compiler_spec_helper.h create mode 100644 spec/compiler/compiler_specs.cpp rename spec/{spec_helper.cpp => compiler/helpers/character_set_helpers.cpp} (68%) create mode 100644 spec/compiler/helpers/character_set_helpers.h rename spec/{ => compiler}/helpers/equals_pointer.h (100%) rename spec/{ => compiler}/helpers/stream_methods.h (100%) create mode 100644 spec/runtime/runtime_spec_helper.h rename spec/{main.cpp => runtime/runtime_specs.cpp} (84%) delete mode 100644 spec/spec_helper.h diff --git a/spec/compiler/build_tables/build_tables_spec.cpp b/spec/compiler/build_tables/build_tables_spec.cpp index 53bcd5da..866aa90c 100644 --- a/spec/compiler/build_tables/build_tables_spec.cpp +++ b/spec/compiler/build_tables/build_tables_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "prepared_grammar.h" #include "build_tables/build_tables.h" #include diff --git a/spec/compiler/build_tables/first_set_spec.cpp b/spec/compiler/build_tables/first_set_spec.cpp index 51be7b98..778bf3e1 100644 --- a/spec/compiler/build_tables/first_set_spec.cpp +++ b/spec/compiler/build_tables/first_set_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "prepared_grammar.h" #include "build_tables/first_set.h" diff --git a/spec/compiler/build_tables/follow_sets_spec.cpp b/spec/compiler/build_tables/follow_sets_spec.cpp index be538084..ba67078f 100644 --- a/spec/compiler/build_tables/follow_sets_spec.cpp +++ b/spec/compiler/build_tables/follow_sets_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "prepared_grammar.h" #include "build_tables/follow_sets.h" diff --git a/spec/compiler/build_tables/item_set_closure_spec.cpp b/spec/compiler/build_tables/item_set_closure_spec.cpp index 891edfc7..19e7e9b0 100644 --- a/spec/compiler/build_tables/item_set_closure_spec.cpp +++ b/spec/compiler/build_tables/item_set_closure_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "prepared_grammar.h" #include "build_tables/item_set_closure.h" #include "build_tables/item_set_transitions.h" diff --git a/spec/compiler/build_tables/rule_can_be_blank_spec.cpp b/spec/compiler/build_tables/rule_can_be_blank_spec.cpp index ae40ca2c..deffa12f 100644 --- a/spec/compiler/build_tables/rule_can_be_blank_spec.cpp +++ b/spec/compiler/build_tables/rule_can_be_blank_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "build_tables/rule_can_be_blank.h" using namespace rules; diff --git a/spec/compiler/build_tables/rule_transitions_spec.cpp b/spec/compiler/build_tables/rule_transitions_spec.cpp index c032b95e..f65264b0 100644 --- a/spec/compiler/build_tables/rule_transitions_spec.cpp +++ b/spec/compiler/build_tables/rule_transitions_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "build_tables/rule_transitions.h" using namespace rules; diff --git a/spec/compiler/compile_examples.cpp b/spec/compiler/compile_examples.cpp index 2bee5840..be21c8be 100644 --- a/spec/compiler/compile_examples.cpp +++ b/spec/compiler/compile_examples.cpp @@ -1,13 +1,19 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "../../examples/grammars/json.hpp" #include "../../examples/grammars/arithmetic.hpp" #include +static string src_dir() { + const char * dir = getenv("TREESITTER_DIR"); + if (!dir) dir = getenv("PWD"); + return dir; +} + START_TEST describe("compiling the example grammars", []() { string example_parser_dir = src_dir() + "/examples/parsers/"; - + it("compiles the arithmetic grammar", [&]() { Grammar grammar = test_grammars::arithmetic(); ofstream(example_parser_dir + "arithmetic.c") << compile(grammar, "arithmetic"); @@ -19,4 +25,4 @@ describe("compiling the example grammars", []() { }); }); -END_TEST \ No newline at end of file +END_TEST diff --git a/spec/compiler/compiler_spec_helper.h b/spec/compiler/compiler_spec_helper.h new file mode 100644 index 00000000..5d20df01 --- /dev/null +++ b/spec/compiler/compiler_spec_helper.h @@ -0,0 +1,18 @@ +#ifndef __tree_sitter_compiler_spec_helper_h__ +#define __tree_sitter_compiler_spec_helper_h__ + +#include "bandit/bandit.h" +#include "helpers/stream_methods.h" +#include "helpers/equals_pointer.h" +#include "helpers/character_set_helpers.h" +#include "tree_sitter/compiler.h" +#include "rules/character_set.h" + +using namespace tree_sitter; +using namespace std; +using namespace bandit; + +#define START_TEST go_bandit([]() { +#define END_TEST }); + +#endif diff --git a/spec/compiler/compiler_specs.cpp b/spec/compiler/compiler_specs.cpp new file mode 100644 index 00000000..3fe6d2ef --- /dev/null +++ b/spec/compiler/compiler_specs.cpp @@ -0,0 +1,12 @@ +#include "compiler_spec_helper.h" + +int main(int argc, char *argv[]) +{ + const char *args[] = { + "", + "--no-color", + "--only=" + "" + }; + return bandit::run(4, const_cast(args)); +} diff --git a/spec/spec_helper.cpp b/spec/compiler/helpers/character_set_helpers.cpp similarity index 68% rename from spec/spec_helper.cpp rename to spec/compiler/helpers/character_set_helpers.cpp index 0266266c..9f0a5d35 100644 --- a/spec/spec_helper.cpp +++ b/spec/compiler/helpers/character_set_helpers.cpp @@ -1,18 +1,15 @@ -#include "spec_helper.h" -#include "rules/character_set.h" - -string src_dir() { - const char * dir = getenv("TREESITTER_DIR"); - if (!dir) dir = getenv("PWD"); - return dir; -} +#include "character_set_helpers.h" +#include namespace tree_sitter { + using std::make_shared; + using std::set; + namespace rules { rule_ptr character(const set &ranges) { return make_shared(ranges); } - + rule_ptr character(const set &ranges, bool sign) { if (sign) return character(ranges); @@ -20,4 +17,4 @@ namespace tree_sitter { return CharacterSet(ranges).complement().copy(); } } -} \ No newline at end of file +} diff --git a/spec/compiler/helpers/character_set_helpers.h b/spec/compiler/helpers/character_set_helpers.h new file mode 100644 index 00000000..27804669 --- /dev/null +++ b/spec/compiler/helpers/character_set_helpers.h @@ -0,0 +1,14 @@ +#ifndef __tree_sitter__character_set_helpers__ +#define __tree_sitter__character_set_helpers__ + +#include "tree_sitter/compiler.h" +#include "rules/character_set.h" + +namespace tree_sitter { + namespace rules { + rule_ptr character(const std::set &ranges); + rule_ptr character(const std::set &ranges, bool sign); + } +} + +#endif diff --git a/spec/helpers/equals_pointer.h b/spec/compiler/helpers/equals_pointer.h similarity index 100% rename from spec/helpers/equals_pointer.h rename to spec/compiler/helpers/equals_pointer.h diff --git a/spec/helpers/stream_methods.h b/spec/compiler/helpers/stream_methods.h similarity index 100% rename from spec/helpers/stream_methods.h rename to spec/compiler/helpers/stream_methods.h diff --git a/spec/compiler/prepare_grammar_spec.cpp b/spec/compiler/prepare_grammar_spec.cpp index 448e68fd..ceb31657 100644 --- a/spec/compiler/prepare_grammar_spec.cpp +++ b/spec/compiler/prepare_grammar_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "prepared_grammar.h" #include "prepare_grammar/prepare_grammar.h" #include "rules/symbol.h" diff --git a/spec/compiler/rules/character_set_spec.cpp b/spec/compiler/rules/character_set_spec.cpp index b92e35c0..4d90e4a6 100644 --- a/spec/compiler/rules/character_set_spec.cpp +++ b/spec/compiler/rules/character_set_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "rules/character_set.h" using namespace rules; diff --git a/spec/compiler/rules/pattern_spec.cpp b/spec/compiler/rules/pattern_spec.cpp index acda7700..5291acac 100644 --- a/spec/compiler/rules/pattern_spec.cpp +++ b/spec/compiler/rules/pattern_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "rules/pattern.h" #include "rules/character_set.h" diff --git a/spec/compiler/rules/rules_spec.cpp b/spec/compiler/rules/rules_spec.cpp index 0fec228d..c0c6ea90 100644 --- a/spec/compiler/rules/rules_spec.cpp +++ b/spec/compiler/rules/rules_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "compiler_spec_helper.h" #include "rules/rule.h" using namespace rules; diff --git a/spec/runtime/arithmetic_spec.cpp b/spec/runtime/arithmetic_spec.cpp index e59c5a4e..09e56272 100644 --- a/spec/runtime/arithmetic_spec.cpp +++ b/spec/runtime/arithmetic_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "runtime_spec_helper.h" extern ts_parse_config ts_parse_config_arithmetic; diff --git a/spec/runtime/json_spec.cpp b/spec/runtime/json_spec.cpp index 1ed90279..2d585f9a 100644 --- a/spec/runtime/json_spec.cpp +++ b/spec/runtime/json_spec.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "runtime_spec_helper.h" extern ts_parse_config ts_parse_config_json; diff --git a/spec/runtime/runtime_spec_helper.h b/spec/runtime/runtime_spec_helper.h new file mode 100644 index 00000000..e3262b2b --- /dev/null +++ b/spec/runtime/runtime_spec_helper.h @@ -0,0 +1,13 @@ +#ifndef __tree_sitter_runtime_spec_helper_h__ +#define __tree_sitter_runtime_spec_helper_h__ + +#include "bandit/bandit.h" +#include "tree_sitter/runtime.h" + +using namespace std; +using namespace bandit; + +#define START_TEST go_bandit([]() { +#define END_TEST }); + +#endif diff --git a/spec/main.cpp b/spec/runtime/runtime_specs.cpp similarity index 84% rename from spec/main.cpp rename to spec/runtime/runtime_specs.cpp index 73ef9ab9..2c1acd35 100644 --- a/spec/main.cpp +++ b/spec/runtime/runtime_specs.cpp @@ -1,4 +1,4 @@ -#include "spec_helper.h" +#include "runtime_spec_helper.h" int main(int argc, char *argv[]) { @@ -9,4 +9,4 @@ int main(int argc, char *argv[]) "" }; return bandit::run(4, const_cast(args)); -} \ No newline at end of file +} diff --git a/spec/runtime/tree_spec.cpp b/spec/runtime/tree_spec.cpp index fa3fc24b..1ef26c0b 100644 --- a/spec/runtime/tree_spec.cpp +++ b/spec/runtime/tree_spec.cpp @@ -1,8 +1,8 @@ -#include "spec_helper.h" +#include "runtime_spec_helper.h" static ts_tree ** tree_array(vector trees) { ts_tree ** result = (ts_tree **)calloc(trees.size(), sizeof(ts_tree *)); - for (int i = 0; i < trees.size(); i++) + for (size_t i = 0; i < trees.size(); i++) result[i] = trees[i]; return result; } @@ -14,12 +14,12 @@ static const char *names[] = { "cat", "dog", "pig" }; describe("trees", []() { ts_tree *tree1, *parent1; - + before_each([&]() { tree1 = ts_tree_make_leaf(cat, 0, 0); parent1 = ts_tree_make_node(dog, 1, tree_array({ tree1 }), 0, 0); }); - + after_each([&]() { ts_tree_release(tree1); ts_tree_release(parent1); @@ -29,20 +29,20 @@ describe("trees", []() { it("returns true for identical trees", [&]() { ts_tree *tree2 = ts_tree_make_leaf(cat, 0, 0); AssertThat(ts_tree_equals(tree1, tree2), Equals(1)); - + ts_tree *parent2 = ts_tree_make_node(dog, 1, tree_array({ tree2 }), 0, 0); AssertThat(ts_tree_equals(parent1, parent2), Equals(1)); - + ts_tree_release(tree2); ts_tree_release(parent2); }); - + it("returns false for trees with different symbols", [&]() { ts_tree *tree2 = ts_tree_make_leaf(pig, 0, 0); AssertThat(ts_tree_equals(tree1, tree2), Equals(0)); ts_tree_release(tree2); }); - + it("returns false for trees with different children", [&]() { ts_tree *tree2 = ts_tree_make_leaf(pig, 0, 0); ts_tree *parent2 = ts_tree_make_node(dog, 1, tree_array({ tree2 }), 0, 0); @@ -52,7 +52,7 @@ describe("trees", []() { ts_tree_release(parent2); }); }); - + describe("serialization", [&]() { it("returns a readable string", [&]() { AssertThat(string(ts_tree_string(tree1, names)), Equals("(cat)")); @@ -61,4 +61,4 @@ describe("trees", []() { }); }); -END_TEST \ No newline at end of file +END_TEST diff --git a/spec/spec_helper.h b/spec/spec_helper.h deleted file mode 100644 index 954be878..00000000 --- a/spec/spec_helper.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef __tree_sitter_spec_helper_h__ -#define __tree_sitter_spec_helper_h__ - -#include "bandit/bandit.h" -#include "helpers/stream_methods.h" -#include "helpers/equals_pointer.h" -#include "tree_sitter/compiler.h" -#include "tree_sitter/runtime.h" -#include "rules/character_range.h" - -using namespace tree_sitter; -using namespace std; -using namespace bandit; - -#define START_TEST go_bandit([]() { -#define END_TEST }); - -string src_dir(); - -namespace tree_sitter { - namespace rules { - rule_ptr character(const std::set &matches); - rule_ptr character(const std::set &matches, bool); - } -} - -#endif diff --git a/tree_sitter.gyp b/tree_sitter.gyp index 48f0ca0a..ab9b7b16 100644 --- a/tree_sitter.gyp +++ b/tree_sitter.gyp @@ -46,10 +46,9 @@ 'include_dirs': [ 'externals/bandit', 'src/compiler', - 'spec', + 'spec/compiler', ], 'sources': [ - '