Test
This commit is contained in:
		
							
								
								
									
										14
									
								
								pkg/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								pkg/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func Calculate(x int) (result int) {
 | 
			
		||||
	result = x + 2
 | 
			
		||||
	return result
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fmt.Println("Go Calculate")
 | 
			
		||||
	result := Calculate(2)
 | 
			
		||||
	fmt.Println(result)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								pkg/main_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								pkg/main_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
func TestCalculate(t *testing.T) {
 | 
			
		||||
	if Calculate(2) != 4 {
 | 
			
		||||
		t.Error("Expected 2 + 2 equal 4")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestTableCalculate(t *testing.T) {
 | 
			
		||||
	var tests = []struct {
 | 
			
		||||
		input    int
 | 
			
		||||
		expected int
 | 
			
		||||
	}{
 | 
			
		||||
		{2, 4},
 | 
			
		||||
		{-3, -1},
 | 
			
		||||
		{0, 2},
 | 
			
		||||
		{99989988, 99989990},
 | 
			
		||||
	}
 | 
			
		||||
	for _, test := range tests {
 | 
			
		||||
		if output := Calculate(test.input); output != test.expected {
 | 
			
		||||
			t.Errorf("Test Failed: %d, inputted: %d expected, recieved: %d", test.input, test.expected, output)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user