Skip to content

Commit

Permalink
[Frontend-go] Add unit testing on combined sample (#1981)
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Chan <[email protected]>
  • Loading branch information
arthurscchan authored Jan 15, 2025
1 parent a854471 commit 5775eee
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
119 changes: 119 additions & 0 deletions src/test/data/source-code/go/test-project-8/fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package combined

import (
"fmt"
"strconv"
"testing"
"time"
)

type Shape struct {
Type string
Radius float64
Width float64
Height float64
}

func (s Shape) Area() float64 {
if s.Type == "Circle" {
return 3.14 * s.Radius * s.Radius
} else if s.Type == "Rectangle" {
return s.Width * s.Height
}
return 0
}

func (s Shape) Perimeter() float64 {
if s.Type == "Circle" {
return 2 * 3.14 * s.Radius
} else if s.Type == "Rectangle" {
return 2 * (s.Width + s.Height)
}
return 0
}

type Person struct {
Name string
Age int
}

func (p Person) Greet() string {
return fmt.Sprintf("Hello, my name is %s and I am %d years old.", p.Name, p.Age)
}

func (p Person) GoodBye() string {
return fmt.Sprintf("Bye. See you next time.")
}

func unreachableGoroutine() {
for i := 0; i < 5; i++ {
processValue(i)
}
}

func processValue(value int) {
fmt.Printf("Processing value: %d\n", value)
}

func FuzzCombined(f *testing.F) {
f.Fuzz(func(t *testing.T, radiusString, widthString, heightString, name string, ageString string) {
radius, err := strconv.ParseFloat(radiusString, 64)
if err != nil {
return
}

width, err := strconv.ParseFloat(widthString, 64)
if err != nil {
return
}

height, err := strconv.ParseFloat(heightString, 64)
if err != nil {
return
}

age, err := strconv.Atoi(ageString)
if err != nil {
return
}

shapes := []Shape{
{Type: "Circle", Radius: radius},
{Type: "Rectangle", Width: width, Height: height},
}

p := Person{Name: name, Age: age}

ch := make(chan string)
go func() {
ch <- p.Greet()
for _, shape := range shapes {
ch <- fmt.Sprintf("Shape: %s, Area: %.2f, Perimeter: %.2f", shape.Type, shape.Area(), shape.Perimeter())
}
close(ch)
}()

for msg := range ch {
fmt.Println(msg)
}

for i := 0; i < len(shapes); i++ {
fmt.Printf("Processing shape %d - Area: %.2f\n", i, shapes[i].Area())
}
})
}
13 changes: 12 additions & 1 deletion src/test/test_frontends_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,18 @@ def test_tree_sitter_go_sample8():

# Project check
harness = project.get_source_codes_with_harnesses()
assert len(harness) == 0
assert len(harness) == 1

functions_reached = project.get_reachable_functions(harness[0].source_file, harness[0])

# Callsite check
assert 'Person.Greet' in functions_reached
assert 'Shape.Area' in functions_reached
assert 'Shape.Perimeter' in functions_reached
assert 'close' in functions_reached
assert 'unreachableGoroutine' not in functions_reached
assert 'processValue' not in functions_reached
assert 'Person.GoodBye' not in functions_reached


def test_tree_sitter_go_sample9():
Expand Down

0 comments on commit 5775eee

Please sign in to comment.