Skip to content

Commit

Permalink
testrunner: refactored givenACodeSampleToTokenize into `SimpleToken…
Browse files Browse the repository at this point in the history
…izer` and `SimpleTokenList`
  • Loading branch information
firewave committed Mar 5, 2024
1 parent 14833a4 commit 49d703c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 94 deletions.
44 changes: 33 additions & 11 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "tokenlist.h"

#include <cstddef>
#include <stdexcept>
#include <sstream> // IWYU pragma: keep
#include <string>
#include <vector>
Expand All @@ -35,19 +36,13 @@ namespace simplecpp {
struct DUI;
}

class givenACodeSampleToTokenize {
private:
const Settings settings;
Tokenizer tokenizer;

class SimpleTokenizer {
public:
explicit givenACodeSampleToTokenize(const char sample[], bool createOnly = false, bool cpp = true)
: tokenizer(settings, nullptr) {
explicit SimpleTokenizer(const char sample[], bool cpp = true)
{
std::istringstream iss(sample);
if (createOnly)
tokenizer.list.createTokens(iss, cpp ? "test.cpp" : "test.c");
else
tokenizer.tokenize(iss, cpp ? "test.cpp" : "test.c");
if (!tokenizer.tokenize(iss, cpp ? "test.cpp" : "test.c"))
throw std::runtime_error("creating tokens failed");
}

Token* tokens() {
Expand All @@ -57,6 +52,33 @@ class givenACodeSampleToTokenize {
const Token* tokens() const {
return tokenizer.tokens();
}

private:
const Settings settings;
Tokenizer tokenizer{settings, nullptr};
};

class SimpleTokenList
{
public:
explicit SimpleTokenList(const char code[], bool cpp = true)
{
std::istringstream iss(code);
if (!list.createTokens(iss, cpp ? "test.cpp" : "test.c"))
throw std::runtime_error("creating tokens failed");
}

Token* tokens() {
return list.front();
}

const Token* tokens() const {
return list.front();
}

private:
const Settings settings;
TokenList list{&settings};
};


Expand Down
16 changes: 8 additions & 8 deletions test/testlibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ class TestLibrary : public TestFixture {
ASSERT_EQUALS(C.arrayLike_indexOp, true);

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

{
givenACodeSampleToTokenize var("std::A<int>::size_type a_s;");
SimpleTokenizer var("std::A<int>::size_type a_s;");
ASSERT(!library.detectContainer(var.tokens()));
ASSERT(!library.detectIterator(var.tokens()));
ASSERT(!library.detectContainerOrIterator(var.tokens()));
}

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

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

{
givenACodeSampleToTokenize var("std::B<int>::size_type b_s;");
SimpleTokenizer var("std::B<int>::size_type b_s;");
ASSERT(!library.detectContainer(var.tokens()));
ASSERT(!library.detectIterator(var.tokens()));
ASSERT(!library.detectContainerOrIterator(var.tokens()));
}

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

{
givenACodeSampleToTokenize var("C c;");
SimpleTokenizer var("C c;");
ASSERT(!library.detectContainer(var.tokens()));
ASSERT(!library.detectIterator(var.tokens()));
ASSERT(!library.detectContainerOrIterator(var.tokens()));
}

{
givenACodeSampleToTokenize var("D d;");
SimpleTokenizer var("D d;");
ASSERT(!library.detectContainer(var.tokens()));
ASSERT(!library.detectIterator(var.tokens()));
ASSERT(!library.detectContainerOrIterator(var.tokens()));
Expand Down
2 changes: 1 addition & 1 deletion test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ class TestSymbolDatabase : public TestFixture {
}
{
reset();
givenACodeSampleToTokenize constpointer("const int* p;");
SimpleTokenizer constpointer("const int* p;");
Variable v2(constpointer.tokens()->tokAt(3), constpointer.tokens()->next(), constpointer.tokens()->tokAt(2), 0, AccessControl::Public, nullptr, nullptr, &settings1);
ASSERT(false == v2.isArray());
ASSERT(true == v2.isPointer());
Expand Down
Loading

0 comments on commit 49d703c

Please sign in to comment.