Skip to content

Commit 49d703c

Browse files
committed
testrunner: refactored givenACodeSampleToTokenize into SimpleTokenizer and SimpleTokenList
1 parent 14833a4 commit 49d703c

File tree

4 files changed

+116
-94
lines changed

4 files changed

+116
-94
lines changed

test/helpers.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "tokenlist.h"
2525

2626
#include <cstddef>
27+
#include <stdexcept>
2728
#include <sstream> // IWYU pragma: keep
2829
#include <string>
2930
#include <vector>
@@ -35,19 +36,13 @@ namespace simplecpp {
3536
struct DUI;
3637
}
3738

38-
class givenACodeSampleToTokenize {
39-
private:
40-
const Settings settings;
41-
Tokenizer tokenizer;
42-
39+
class SimpleTokenizer {
4340
public:
44-
explicit givenACodeSampleToTokenize(const char sample[], bool createOnly = false, bool cpp = true)
45-
: tokenizer(settings, nullptr) {
41+
explicit SimpleTokenizer(const char sample[], bool cpp = true)
42+
{
4643
std::istringstream iss(sample);
47-
if (createOnly)
48-
tokenizer.list.createTokens(iss, cpp ? "test.cpp" : "test.c");
49-
else
50-
tokenizer.tokenize(iss, cpp ? "test.cpp" : "test.c");
44+
if (!tokenizer.tokenize(iss, cpp ? "test.cpp" : "test.c"))
45+
throw std::runtime_error("creating tokens failed");
5146
}
5247

5348
Token* tokens() {
@@ -57,6 +52,33 @@ class givenACodeSampleToTokenize {
5752
const Token* tokens() const {
5853
return tokenizer.tokens();
5954
}
55+
56+
private:
57+
const Settings settings;
58+
Tokenizer tokenizer{settings, nullptr};
59+
};
60+
61+
class SimpleTokenList
62+
{
63+
public:
64+
explicit SimpleTokenList(const char code[], bool cpp = true)
65+
{
66+
std::istringstream iss(code);
67+
if (!list.createTokens(iss, cpp ? "test.cpp" : "test.c"))
68+
throw std::runtime_error("creating tokens failed");
69+
}
70+
71+
Token* tokens() {
72+
return list.front();
73+
}
74+
75+
const Token* tokens() const {
76+
return list.front();
77+
}
78+
79+
private:
80+
const Settings settings;
81+
TokenList list{&settings};
6082
};
6183

6284

test/testlibrary.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ class TestLibrary : public TestFixture {
871871
ASSERT_EQUALS(C.arrayLike_indexOp, true);
872872

873873
{
874-
givenACodeSampleToTokenize var("std::A<int> a;");
874+
SimpleTokenizer var("std::A<int> a;");
875875
ASSERT_EQUALS(&A, library.detectContainer(var.tokens()));
876876
ASSERT(!library.detectIterator(var.tokens()));
877877
bool isIterator;
@@ -880,14 +880,14 @@ class TestLibrary : public TestFixture {
880880
}
881881

882882
{
883-
givenACodeSampleToTokenize var("std::A<int>::size_type a_s;");
883+
SimpleTokenizer var("std::A<int>::size_type a_s;");
884884
ASSERT(!library.detectContainer(var.tokens()));
885885
ASSERT(!library.detectIterator(var.tokens()));
886886
ASSERT(!library.detectContainerOrIterator(var.tokens()));
887887
}
888888

889889
{
890-
givenACodeSampleToTokenize var("std::A<int>::iterator a_it;");
890+
SimpleTokenizer var("std::A<int>::iterator a_it;");
891891
ASSERT(!library.detectContainer(var.tokens()));
892892
ASSERT_EQUALS(&A, library.detectIterator(var.tokens()));
893893
bool isIterator;
@@ -896,7 +896,7 @@ class TestLibrary : public TestFixture {
896896
}
897897

898898
{
899-
givenACodeSampleToTokenize var("std::B<int> b;");
899+
SimpleTokenizer var("std::B<int> b;");
900900
ASSERT_EQUALS(&B, library.detectContainer(var.tokens()));
901901
ASSERT(!library.detectIterator(var.tokens()));
902902
bool isIterator;
@@ -905,14 +905,14 @@ class TestLibrary : public TestFixture {
905905
}
906906

907907
{
908-
givenACodeSampleToTokenize var("std::B<int>::size_type b_s;");
908+
SimpleTokenizer var("std::B<int>::size_type b_s;");
909909
ASSERT(!library.detectContainer(var.tokens()));
910910
ASSERT(!library.detectIterator(var.tokens()));
911911
ASSERT(!library.detectContainerOrIterator(var.tokens()));
912912
}
913913

914914
{
915-
givenACodeSampleToTokenize var("std::B<int>::iterator b_it;");
915+
SimpleTokenizer var("std::B<int>::iterator b_it;");
916916
ASSERT(!library.detectContainer(var.tokens()));
917917
ASSERT_EQUALS(&B, library.detectIterator(var.tokens()));
918918
bool isIterator;
@@ -921,14 +921,14 @@ class TestLibrary : public TestFixture {
921921
}
922922

923923
{
924-
givenACodeSampleToTokenize var("C c;");
924+
SimpleTokenizer var("C c;");
925925
ASSERT(!library.detectContainer(var.tokens()));
926926
ASSERT(!library.detectIterator(var.tokens()));
927927
ASSERT(!library.detectContainerOrIterator(var.tokens()));
928928
}
929929

930930
{
931-
givenACodeSampleToTokenize var("D d;");
931+
SimpleTokenizer var("D d;");
932932
ASSERT(!library.detectContainer(var.tokens()));
933933
ASSERT(!library.detectIterator(var.tokens()));
934934
ASSERT(!library.detectContainerOrIterator(var.tokens()));

test/testsymboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ class TestSymbolDatabase : public TestFixture {
816816
}
817817
{
818818
reset();
819-
givenACodeSampleToTokenize constpointer("const int* p;");
819+
SimpleTokenizer constpointer("const int* p;");
820820
Variable v2(constpointer.tokens()->tokAt(3), constpointer.tokens()->next(), constpointer.tokens()->tokAt(2), 0, AccessControl::Public, nullptr, nullptr, &settings1);
821821
ASSERT(false == v2.isArray());
822822
ASSERT(true == v2.isPointer());

0 commit comments

Comments
 (0)