Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekazakov committed May 20, 2024
1 parent 321f066 commit 69fbf2a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
1 change: 1 addition & 0 deletions Source/Viewer/include/Viewer/Highlighting/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class Document final : public Scintilla::IDocument
private:
std::string_view m_Text;
std::vector<uint32_t> m_Lines;
std::vector<int> m_LineStates;
std::vector<char> m_Styles;
Sci_Position m_StylingPosition = 0;
};
Expand Down
12 changes: 6 additions & 6 deletions Source/Viewer/source/Highlighting/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Document::Document(const std::string_view _text) : m_Text(_text), m_Styles(_text
m_Lines.push_back(static_cast<uint32_t>(i + 1));
}
}

m_LineStates.resize(m_Lines.size() + 1);
}

Document::~Document() = default;
Expand Down Expand Up @@ -123,16 +125,14 @@ int Document::SetLevel(Sci_Position /*_line*/, int /*_level*/) noexcept
return 0;
}

int Document::GetLineState(Sci_Position /*_line*/) const noexcept
int Document::GetLineState(Sci_Position _line) const noexcept
{
abort();
return 0;
return m_LineStates.at(_line);
}

int Document::SetLineState(Sci_Position /*_line*/, int /*_state*/) noexcept
int Document::SetLineState(Sci_Position _line, int _state) noexcept
{
abort();
return 0;
return m_LineStates.at(_line) = _state;
}

int Document::GetLineIndentation(Sci_Position /*_line*/) noexcept
Expand Down
60 changes: 50 additions & 10 deletions Source/Viewer/tests/hlHighlighter_UT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
#include "Tests.h"
#include "Highlighting/Highlighter.h"
#include <lexilla/SciLexer.h>
#include <robin_hood.h>

using namespace nc::viewer::hl;

#define PREFIX "hl::Highlighter "

[[clang::no_destroy]] static const robin_hood::unordered_flat_map<char, Style> m{
{'D', Style::Default},
{'C', Style::Comment},
{'W', Style::Keyword},
{'P', Style::Preprocessor},
{'N', Style::Number},
{'O', Style::Operator},
{'I', Style::Identifier},
{'S', Style::String},
};

TEST_CASE(PREFIX "Regular use with C++ lexer")
{
LexerSettings set;
Expand All @@ -22,16 +34,6 @@ TEST_CASE(PREFIX "Regular use with C++ lexer")
set.mapping.SetMapping(SCE_C_IDENTIFIER, Style::Identifier);
set.mapping.SetMapping(SCE_C_STRING, Style::String);

std::unordered_map<char, Style> m;
m['D'] = Style::Default;
m['C'] = Style::Comment;
m['W'] = Style::Keyword;
m['P'] = Style::Preprocessor;
m['N'] = Style::Number;
m['O'] = Style::Operator;
m['I'] = Style::Identifier;
m['S'] = Style::String;

const std::string src =
R"(#pragma once
/*Hey!*/
Expand All @@ -50,3 +52,41 @@ int hello = 10;)";
CHECK(hl[i] == m.at(hl_exp[i]));
}
}

TEST_CASE(PREFIX "Regular use with YAML lexer")
{
LexerSettings set;
set.name = "yaml";
set.mapping.SetMapping(SCE_YAML_DEFAULT, Style::Default);
set.mapping.SetMapping(SCE_YAML_COMMENT, Style::Comment);
set.mapping.SetMapping(SCE_YAML_KEYWORD, Style::Keyword);
set.mapping.SetMapping(SCE_YAML_NUMBER, Style::Number);
set.mapping.SetMapping(SCE_YAML_REFERENCE, Style::Identifier);
set.mapping.SetMapping(SCE_YAML_DOCUMENT, Style::Identifier);
set.mapping.SetMapping(SCE_YAML_OPERATOR, Style::Operator);
set.mapping.SetMapping(SCE_YAML_IDENTIFIER, Style::Identifier);
set.mapping.SetMapping(SCE_YAML_TEXT, Style::String);

const std::string src =
R"(name: Build and Test #Hey!
on:
push:
paths-ignore:
- '.github/ISSUE_TEMPLATE/**')";

const std::string hl_exp = "IIIIODDDDDDDDDDDDDDDDCCCCCC"
"IIOD"
"IIIIIIOD"
"IIIIIIIIIIIIIIIIOD"
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";

REQUIRE(src.length() == hl_exp.length());

Highlighter highlighter(set);
std::vector<Style> hl = highlighter.Highlight(src);
REQUIRE(hl.size() == hl_exp.size());

for( size_t i = 0; i < hl.size(); ++i ) {
CHECK(hl[i] == m.at(hl_exp[i]));
}
}

0 comments on commit 69fbf2a

Please sign in to comment.