Started unit testing

This commit is contained in:
Robert Bendun 2022-04-24 16:09:55 +02:00
parent 05c8fd05b2
commit bf1bec4b44
8 changed files with 110 additions and 10 deletions

View File

@ -2,6 +2,7 @@ image: gcc
stages: stages:
- build - build
- test
build: build:
stage: build stage: build
@ -10,3 +11,9 @@ build:
artifacts: artifacts:
paths: paths:
- main - main
stage: test
unit-testing:
stage: test
script:
- make unit-tests

View File

@ -1,18 +1,25 @@
MAKEFLAGS="-j $(grep -c ^processor /proc/cpuinfo)" MAKEFLAGS="-j $(grep -c ^processor /proc/cpuinfo)"
CXXFLAGS=-std=c++20 -Wall -Wextra -O2 -Werror=switch CXXFLAGS=-std=c++20 -Wall -Wextra -O2 -Werror=switch
CPPFLAGS=-Ilib/expected/ -Ilib/ut/ CPPFLAGS=-Ilib/expected/ -Ilib/ut/ -Isrc/
Obj=bin/lexer.o \ Obj=bin/lexer.o \
bin/errors.o \ bin/errors.o
bin/main.o
all: bin/musique bin/unit-tests
all: bin/musique
bin/%.o: src/%.cc src/*.hh bin/%.o: src/%.cc src/*.hh
g++ $(CXXFLAGS) $(CPPFLAGS) -o $@ $< -c g++ $(CXXFLAGS) $(CPPFLAGS) -o $@ $< -c
bin/musique: $(Obj) src/*.hh bin/musique: $(Obj) bin/main.o src/*.hh
g++ $(CXXFLAGS) $(CPPFLAGS) -o $@ $(Obj) g++ $(CXXFLAGS) $(CPPFLAGS) -o $@ $(Obj) bin/main.o
.PHONY: unit-tests
unit-tests: bin/unit-tests
./$<
bin/unit-tests: src/tests/*.cc $(Obj)
g++ $(CXXFLAGS) $(CPPFLAGS) -o $@ $^
clean: clean:
rm -rf bin rm -rf bin

View File

@ -1,4 +1,9 @@
#include "musique.hh" #include <musique.hh>
bool Error::operator==(errors::Type type)
{
return this->type == type;
}
std::ostream& operator<<(std::ostream& os, Error const&) std::ostream& operator<<(std::ostream& os, Error const&)
{ {

View File

@ -1,4 +1,4 @@
#include "musique.hh" #include <musique.hh>
auto Lexer::next_token() -> Result<Token> auto Lexer::next_token() -> Result<Token>
{ {

View File

@ -1,5 +1,5 @@
#include <iostream> #include <iostream>
#include "musique.hh" #include <musique.hh>
std::string_view Source = R"musique( std::string_view Source = R"musique(
nums = [ 1 2 3 ] nums = [ 1 2 3 ]

View File

@ -15,10 +15,21 @@ using i16 = std::int16_t;
using i32 = std::int32_t; using i32 = std::int32_t;
using i64 = std::int64_t; using i64 = std::int64_t;
namespace errors
{
enum Type
{
End_Of_File
};
}
struct Error struct Error
{ {
std::string_view message; errors::Type type;
Error *child = nullptr; Error *child = nullptr;
bool operator==(errors::Type);
}; };
template<typename T> template<typename T>
@ -59,6 +70,7 @@ struct Token
Close_Paren Close_Paren
}; };
Type type;
std::string_view source; std::string_view source;
}; };

57
src/tests/lex.cc Normal file
View File

@ -0,0 +1,57 @@
#include <boost/ut.hpp>
#include <musique.hh>
using namespace boost::ut;
auto under(auto enumeration) requires std::is_enum_v<decltype(enumeration)>
{
return static_cast<std::underlying_type_t<decltype(enumeration)>>(enumeration);
}
static void expect_token_type(
Token::Type expected_type,
std::string source,
reflection::source_location const& sl = reflection::source_location::current())
{
Lexer lexer{source};
auto result = lexer.next_token();
expect(result.has_value() >> fatal, sl) << "have not parsed any tokens";
expect(eq(under(result->type), under(expected_type)), sl) << "different token type then expected";
}
static void expect_token_type_and_value(
Token::Type expected_type,
std::string_view source,
reflection::source_location const& sl = reflection::source_location::current())
{
Lexer lexer{source};
auto result = lexer.next_token();
expect(result.has_value() >> fatal, sl) << "have not parsed any tokens";
expect(eq(under(result->type), under(expected_type)), sl) << "different token type then expected";
expect(eq(result->source, source)) << "tokenized source is not equal to original";
}
suite lexer_test = [] {
"Empty file"_test = [] {
Lexer lexer{""};
auto result = lexer.next_token();
expect(!result.has_value() >> fatal) << "could not produce any tokens from empty file";
expect(result.error() == errors::End_Of_File) << "could not produce any tokens from empty file";
};
"Simple token types"_test = [] {
expect_token_type(Token::Type::Close_Block, "]");
expect_token_type(Token::Type::Close_Paren, ")");
expect_token_type(Token::Type::Open_Block, "[");
expect_token_type(Token::Type::Open_Paren, "(");
expect_token_type(Token::Type::Variable_Separator, "|");
};
"Numeric tokens"_test = [] {
expect_token_type_and_value(Token::Type::Numeric, "0");
expect_token_type_and_value(Token::Type::Numeric, "123456789");
expect_token_type_and_value(Token::Type::Numeric, ".75");
expect_token_type_and_value(Token::Type::Numeric, "0.75");
expect_token_type_and_value(Token::Type::Numeric, "123456789.123456789");
};
};

12
src/tests/main.cc Normal file
View File

@ -0,0 +1,12 @@
#include <boost/ut.hpp>
int main()
{
using namespace boost::ut;
if (!isatty(STDOUT_FILENO)) {
cfg<override> = options {
.colors = colors { .none = "", .pass = "", .fail = "" }
};
}
}