Skip to content

Commit

Permalink
🐳 add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shivasurya committed Nov 4, 2024
1 parent 5db46e4 commit dbc4f49
Show file tree
Hide file tree
Showing 6 changed files with 569 additions and 0 deletions.
146 changes: 146 additions & 0 deletions sourcecode-parser/model/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package model

import (
"testing"

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

func TestFile_IsSourceFile(t *testing.T) {
tests := []struct {
name string
file File
expected bool
}{
{
name: "Java source file",
file: File{File: "Test.java"},
expected: true,
},
{
name: "Non-source file",
file: File{File: "Test.txt"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.file.IsSourceFile()
assert.Equal(t, tt.expected, result)
})
}
}

func TestJarFile_GetManifestEntryAttributes(t *testing.T) {
jarFile := &JarFile{
ManifestEntryAttributes: map[string]map[string]string{
"entry1": {
"key1": "value1",
"key2": "value2",
},
},
}

t.Run("Existing entry and key", func(t *testing.T) {
value, exists := jarFile.GetManifestEntryAttributes("entry1", "key1")
assert.True(t, exists)
assert.Equal(t, "value1", value)
})

t.Run("Existing entry, non-existing key", func(t *testing.T) {
value, exists := jarFile.GetManifestEntryAttributes("entry1", "nonexistent")
assert.False(t, exists)
assert.Equal(t, "", value)
})

t.Run("Non-existing entry", func(t *testing.T) {
value, exists := jarFile.GetManifestEntryAttributes("nonexistent", "key1")
assert.False(t, exists)
assert.Equal(t, "", value)
})
}

func TestJarFile_GetManifestMainAttributes(t *testing.T) {
jarFile := &JarFile{
ManifestMainAttributes: map[string]string{
"key1": "value1",
"key2": "value2",
},
}

t.Run("Existing key", func(t *testing.T) {
value, exists := jarFile.GetManifestMainAttributes("key1")
assert.True(t, exists)
assert.Equal(t, "value1", value)
})

t.Run("Non-existing key", func(t *testing.T) {
value, exists := jarFile.GetManifestMainAttributes("nonexistent")
assert.False(t, exists)
assert.Equal(t, "", value)
})
}

func TestCompilationUnit_HasName(t *testing.T) {
cu := &CompilationUnit{Name: "TestClass"}

t.Run("Matching name", func(t *testing.T) {
assert.True(t, cu.HasName("TestClass"))
})

t.Run("Non-matching name", func(t *testing.T) {
assert.False(t, cu.HasName("OtherClass"))
})

t.Run("Empty name", func(t *testing.T) {
assert.False(t, cu.HasName(""))
})
}

func TestFile_IsJavaSourceFile(t *testing.T) {
tests := []struct {
name string
file File
expected bool
}{
{
name: "Java file",
file: File{File: "Test.java"},
expected: true,
},
{
name: "Non-Java file",
file: File{File: "Test.kt"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.file.IsJavaSourceFile()
assert.Equal(t, tt.expected, result)
})
}
}

func TestFile_IsKotlinSourceFile(t *testing.T) {
tests := []struct {
name string
file File
expected bool
}{
{
name: "Non-Kotlin file",
file: File{File: "Test.java"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.file.IsKotlinSourceFile()
assert.Equal(t, tt.expected, result)
})
}
}
54 changes: 54 additions & 0 deletions sourcecode-parser/model/location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package model

import (
"testing"

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

func TestLocation(t *testing.T) {
t.Run("New location with valid values", func(t *testing.T) {
loc := Location{
File: "test.go",
Line: 42,
}
assert.Equal(t, "test.go", loc.File)
assert.Equal(t, 42, loc.Line)
})

t.Run("New location with empty file", func(t *testing.T) {
loc := Location{
File: "",
Line: 1,
}
assert.Empty(t, loc.File)
assert.Equal(t, 1, loc.Line)
})

t.Run("New location with zero line", func(t *testing.T) {
loc := Location{
File: "main.go",
Line: 0,
}
assert.Equal(t, "main.go", loc.File)
assert.Zero(t, loc.Line)
})

t.Run("New location with negative line", func(t *testing.T) {
loc := Location{
File: "src.go",
Line: -1,
}
assert.Equal(t, "src.go", loc.File)
assert.Equal(t, -1, loc.Line)
})

t.Run("New location with file path", func(t *testing.T) {
loc := Location{
File: "/path/to/file.go",
Line: 100,
}
assert.Equal(t, "/path/to/file.go", loc.File)
assert.Equal(t, 100, loc.Line)
})
}
28 changes: 28 additions & 0 deletions sourcecode-parser/model/member_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package model

import (
"testing"

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

func TestCallable(t *testing.T) {
t.Run("New Callable with name", func(t *testing.T) {
callable := &Callable{
CallableName: "testFunction",
}
assert.Equal(t, "testFunction", callable.CallableName)
})

t.Run("Empty Callable name", func(t *testing.T) {
callable := &Callable{}
assert.Equal(t, "", callable.CallableName)
})

t.Run("Callable with special characters", func(t *testing.T) {
callable := &Callable{
CallableName: "test$Function_123",
}
assert.Equal(t, "test$Function_123", callable.CallableName)
})
}
65 changes: 65 additions & 0 deletions sourcecode-parser/model/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package model

import (
"testing"

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

func TestModule(t *testing.T) {
cu := &CompilationUnit{}
directive := Directive{Directive: "requires java.base"}
module := &Module{
Cu: cu,
Di: directive,
Name: "com.example.module",
isOpen: true,
}

t.Run("GetAPrimaryQlClass", func(t *testing.T) {
assert.Equal(t, "Module", module.GetAPrimaryQlClass())
})

t.Run("GetACompilationUnit", func(t *testing.T) {
assert.Equal(t, cu, module.GetACompilationUnit())
})

t.Run("GetName", func(t *testing.T) {
assert.Equal(t, "com.example.module", module.GetName())
})

t.Run("ToString", func(t *testing.T) {
assert.Equal(t, "com.example.module", module.ToString())
})

t.Run("GetDirective", func(t *testing.T) {
assert.Equal(t, &directive, module.GetDirective())
})

t.Run("IsOpen", func(t *testing.T) {
assert.True(t, module.IsOpen())
})
}

func TestModuleWithEmptyValues(t *testing.T) {
module := &Module{}

t.Run("Empty module properties", func(t *testing.T) {
assert.Nil(t, module.GetACompilationUnit())
assert.Empty(t, module.GetName())
assert.Empty(t, module.ToString())
assert.False(t, module.IsOpen())
})
}

func TestDirective(t *testing.T) {
t.Run("ToString with value", func(t *testing.T) {
directive := &Directive{Directive: "requires transitive java.sql"}
assert.Equal(t, "requires transitive java.sql", directive.ToString())
})

t.Run("ToString empty", func(t *testing.T) {
directive := &Directive{}
assert.Empty(t, directive.ToString())
})
}
Loading

0 comments on commit dbc4f49

Please sign in to comment.