Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Aug 24, 2023
1 parent 5f14b77 commit 1d80307
Show file tree
Hide file tree
Showing 384 changed files with 2,495 additions and 1,552 deletions.
8 changes: 0 additions & 8 deletions pkg/language/chroma.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ func fileHead(filepath string) ([]byte, error) {

// objectiveCWeight determines the weight of objective-c by the provided same folder file extensions.
func objectiveCWeight(weight float32, extensions []string) float32 {
if len(extensions) == 0 {
return weight
}

var matFileExists bool

for _, e := range extensions {
Expand Down Expand Up @@ -248,10 +244,6 @@ func objectiveCWeight(weight float32, extensions []string) float32 {

// matlabWeight determines the weight of matlab by the provided same folder file extensions.
func matlabWeight(weight float32, extensions []string) float32 {
if len(extensions) == 0 {
return weight
}

for _, e := range extensions {
if e == ".mat" {
weight += 0.01
Expand Down
36 changes: 29 additions & 7 deletions pkg/language/chroma/actionscript3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,39 @@ package chroma
import (
"regexp"

"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/lexers"
)

// nolint:gochecknoglobals
var actionscript3AnalyserRe = regexp.MustCompile(`\w+\s*:\s*\w`)

func init() { // nolint: gochecknoinits
lexers.Get("ActionScript 3").SetAnalyser(func(text string) float32 {
if actionscript3AnalyserRe.MatchString(text) {
return 0.3
}
// ActionScript3 lexer.
type ActionScript3 struct{}

return 0
})
// Lexer returns the lexer.
func (l ActionScript3) Lexer() chroma.Lexer {
lexer := lexers.Get(l.Name())
if lexer == nil {
return nil
}

if lexer, ok := lexer.(*chroma.RegexLexer); ok {
lexer.SetAnalyser(func(text string) float32 {
if actionscript3AnalyserRe.MatchString(text) {
return 0.3
}

return 0
})

return lexer
}

return nil
}

// Name returns the name of the lexer.
func (ActionScript3) Name() string {
return "ActionScript 3"
}
17 changes: 7 additions & 10 deletions pkg/language/chroma/actionscript3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"os"
"testing"

"github.com/alecthomas/assert"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/wakatime/wakatime-cli/pkg/language/chroma"

"github.com/stretchr/testify/assert"
)

func TestActionscript3_AnalyseText(t *testing.T) {
func TestActionScript3_AnalyseText(t *testing.T) {
tests := map[string]struct {
Filepath string
Expected float32
Expand All @@ -28,17 +28,14 @@ func TestActionscript3_AnalyseText(t *testing.T) {
},
}

for name, test := range tests {
test := test
l := chroma.ActionScript3{}.Lexer()

for name, test := range tests {
t.Run(name, func(t *testing.T) {
data, err := os.ReadFile(test.Filepath)
assert.NoError(t, err)

analyser, ok := lexers.Get("Actionscript 3").(chroma.Analyser)
assert.True(t, ok)

assert.Equal(t, test.Expected, analyser.AnalyseText(string(data)))
assert.Equal(t, test.Expected, l.AnalyseText(string(data)))
})
}
}
8 changes: 4 additions & 4 deletions pkg/language/chroma/adl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"github.com/alecthomas/chroma/v2"
)

// Adl lexer.
type Adl struct{}
// ADL lexer.
type ADL struct{}

// Lexer returns the lexer.
func (l Adl) Lexer() *chroma.RegexLexer {
func (l ADL) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand All @@ -24,6 +24,6 @@ func (l Adl) Lexer() *chroma.RegexLexer {
}

// Name returns the name of the lexer.
func (Adl) Name() string {
func (ADL) Name() string {
return "ADL"
}
2 changes: 1 addition & 1 deletion pkg/language/chroma/agda.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type Agda struct{}

// Lexer returns the lexer.
func (l Agda) Lexer() *chroma.RegexLexer {
func (l Agda) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/language/chroma/aheui.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type Aheui struct{}

// Lexer returns the lexer.
func (l Aheui) Lexer() *chroma.RegexLexer {
func (l Aheui) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/language/chroma/alloy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type Alloy struct{}

// Lexer returns the lexer.
func (l Alloy) Lexer() *chroma.RegexLexer {
func (l Alloy) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/language/chroma/ambienttalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type AmbientTalk struct{}

// Lexer returns the lexer.
func (l AmbientTalk) Lexer() *chroma.RegexLexer {
func (l AmbientTalk) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down
8 changes: 4 additions & 4 deletions pkg/language/chroma/ampl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"github.com/alecthomas/chroma/v2"
)

// Ampl lexer.
type Ampl struct{}
// AMPL lexer.
type AMPL struct{}

// Lexer returns the lexer.
func (l Ampl) Lexer() *chroma.RegexLexer {
func (l AMPL) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand All @@ -24,6 +24,6 @@ func (l Ampl) Lexer() *chroma.RegexLexer {
}

// Name returns the name of the lexer.
func (Ampl) Name() string {
func (AMPL) Name() string {
return "Ampl"
}
2 changes: 1 addition & 1 deletion pkg/language/chroma/arrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type Arrow struct{}

// Lexer returns the lexer.
func (l Arrow) Lexer() *chroma.RegexLexer {
func (l Arrow) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/language/chroma/aspectj.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type AspectJ struct{}

// Lexer returns the lexer.
func (l AspectJ) Lexer() *chroma.RegexLexer {
func (l AspectJ) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ var (
csharpAspxAnalyzerScriptLanguageRe = regexp.MustCompile(`(?i)script[^>]+language=["\']C#`)
)

// CSharpAspx lexer.
type CSharpAspx struct{}
// AspxCSharp lexer.
type AspxCSharp struct{}

// Lexer returns the lexer.
func (l CSharpAspx) Lexer() *chroma.RegexLexer {
func (l AspxCSharp) Lexer() chroma.Lexer {
lexer := chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down Expand Up @@ -46,6 +46,6 @@ func (l CSharpAspx) Lexer() *chroma.RegexLexer {
}

// Name returns the name of the lexer.
func (CSharpAspx) Name() string {
func (AspxCSharp) Name() string {
return "aspx-cs"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,28 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCSharpAspx_AnalyseText(t *testing.T) {
func TestAspxCSharp_AnalyseText(t *testing.T) {
tests := map[string]struct {
Filepath string
Expected float32
}{
"page language": {
Filepath: "testdata/csharpaspx_page_language.aspx",
Filepath: "testdata/aspxcsharp_page_language.aspx",
Expected: 0.2,
},
"script language": {
Filepath: "testdata/csharpaspx_script_language.aspx",
Filepath: "testdata/aspxcsharp_script_language.aspx",
Expected: 0.15,
},
}

for name, test := range tests {
test := test
l := chroma.AspxCSharp{}.Lexer()

for name, test := range tests {
t.Run(name, func(t *testing.T) {
data, err := os.ReadFile(test.Filepath)
assert.NoError(t, err)

l := chroma.CSharpAspx{}.Lexer()

assert.Equal(t, test.Expected, l.AnalyseText(string(data)))
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"github.com/alecthomas/chroma/v2"
)

// nolint:gochecknoglobals
var (
vbAspxAnalyzerPageLanguageRe = regexp.MustCompile(`(?i)Page\s*Language="Vb"`)
vbAspxAnalyzerScriptLanguageRe = regexp.MustCompile(`(?i)script[^>]+language=["\']vb`)
)

// VBNetAspx lexer.
type VBNetAspx struct{}
// AspxVBNet lexer.
type AspxVBNet struct{}

// Lexer returns the lexer.
func (l VBNetAspx) Lexer() *chroma.RegexLexer {
func (l AspxVBNet) Lexer() chroma.Lexer {
lexer := chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down Expand Up @@ -46,6 +47,6 @@ func (l VBNetAspx) Lexer() *chroma.RegexLexer {
}

// Name returns the name of the lexer.
func (VBNetAspx) Name() string {
func (AspxVBNet) Name() string {
return "aspx-vb"
}
37 changes: 37 additions & 0 deletions pkg/language/chroma/aspxvb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package chroma_test

import (
"os"
"testing"

"github.com/wakatime/wakatime-cli/pkg/language/chroma"

"github.com/stretchr/testify/assert"
)

func TestAspxVBNet_AnalyseText(t *testing.T) {
tests := map[string]struct {
Filepath string
Expected float32
}{
"page language": {
Filepath: "testdata/aspxvbnet_page_language.aspx",
Expected: 0.2,
},
"script language": {
Filepath: "testdata/aspxvbnet_script_language.aspx",
Expected: 0.15,
},
}

l := chroma.AspxVBNet{}.Lexer()

for name, test := range tests {
t.Run(name, func(t *testing.T) {
data, err := os.ReadFile(test.Filepath)
assert.NoError(t, err)

assert.Equal(t, test.Expected, l.AnalyseText(string(data)))
})
}
}
2 changes: 1 addition & 1 deletion pkg/language/chroma/asymptote.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type Asymptote struct{}

// Lexer returns the lexer.
func (l Asymptote) Lexer() *chroma.RegexLexer {
func (l Asymptote) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/language/chroma/augeas.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type Augeas struct{}

// Lexer returns the lexer.
func (l Augeas) Lexer() *chroma.RegexLexer {
func (l Augeas) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand Down
8 changes: 4 additions & 4 deletions pkg/language/chroma/bare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"github.com/alecthomas/chroma/v2"
)

// Bare lexer.
type Bare struct{}
// BARE lexer.
type BARE struct{}

// Lexer returns the lexer.
func (l Bare) Lexer() *chroma.RegexLexer {
func (l BARE) Lexer() chroma.Lexer {
return chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand All @@ -24,6 +24,6 @@ func (l Bare) Lexer() *chroma.RegexLexer {
}

// Name returns the name of the lexer.
func (Bare) Name() string {
func (BARE) Name() string {
return "BARE"
}
8 changes: 4 additions & 4 deletions pkg/language/chroma/bbcbasic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/alecthomas/chroma/v2"
)

// BbcBasic lexer.
type BbcBasic struct{}
// BBCBasic lexer.
type BBCBasic struct{}

// Lexer returns the lexer.
func (l BbcBasic) Lexer() *chroma.RegexLexer {
func (l BBCBasic) Lexer() chroma.Lexer {
lexer := chroma.MustNewLexer(
&chroma.Config{
Name: l.Name(),
Expand All @@ -36,6 +36,6 @@ func (l BbcBasic) Lexer() *chroma.RegexLexer {
}

// Name returns the name of the lexer.
func (BbcBasic) Name() string {
func (BBCBasic) Name() string {
return "BBC Basic"
}
Loading

0 comments on commit 1d80307

Please sign in to comment.