From 49b5f8eb34f3ffe03e012566b55b99681398865d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Henrique=20Guard=C3=A3o=20Gandarez?= <782854+gandarez@users.noreply.github.com> Date: Fri, 18 Aug 2023 09:41:54 -0300 Subject: [PATCH] Add missing C++ tests (#814) --- lexers/cpp_test.go | 34 +++++++++++++++++++++++++++++-- lexers/testdata/cpp_include.cpp | 1 + lexers/testdata/cpp_namespace.cpp | 1 + 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 lexers/testdata/cpp_include.cpp create mode 100644 lexers/testdata/cpp_namespace.cpp diff --git a/lexers/cpp_test.go b/lexers/cpp_test.go index 50ee8c88b..ade2743ee 100644 --- a/lexers/cpp_test.go +++ b/lexers/cpp_test.go @@ -1,14 +1,44 @@ package lexers_test import ( + "os" "testing" - assert "github.com/alecthomas/assert/v2" - "github.com/alecthomas/chroma/v2" "github.com/alecthomas/chroma/v2/lexers" + + "github.com/alecthomas/assert/v2" ) +func TestCpp_AnalyseText(t *testing.T) { + tests := map[string]struct { + Filepath string + Expected float32 + }{ + "include": { + Filepath: "testdata/cpp_include.cpp", + Expected: 0.2, + }, + "namespace": { + Filepath: "testdata/cpp_namespace.cpp", + Expected: 0.4, + }, + } + + for name, test := range tests { + test := test + t.Run(name, func(t *testing.T) { + data, err := os.ReadFile(test.Filepath) + assert.NoError(t, err) + + analyser, ok := lexers.CPP.(chroma.Analyser) + assert.True(t, ok) + + assert.Equal(t, test.Expected, analyser.AnalyseText(string(data))) + }) + } +} + func TestIssue290(t *testing.T) { input := `// 64-bit floats have 53 digits of precision, including the whole-number-part. double a = 0011111110111001100110011001100110011001100110011001100110011010; // imperfect representation of 0.1 diff --git a/lexers/testdata/cpp_include.cpp b/lexers/testdata/cpp_include.cpp new file mode 100644 index 000000000..604782e4d --- /dev/null +++ b/lexers/testdata/cpp_include.cpp @@ -0,0 +1 @@ +#include diff --git a/lexers/testdata/cpp_namespace.cpp b/lexers/testdata/cpp_namespace.cpp new file mode 100644 index 000000000..253f3bf81 --- /dev/null +++ b/lexers/testdata/cpp_namespace.cpp @@ -0,0 +1 @@ +using namespace std;