Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nexryai committed Nov 14, 2023
1 parent d7ee76c commit 1c75f29
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
name: Test build

on:
pull_request:
types:
- opened
- synchronize
push:
branches:
- *
- main

jobs:
release:
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Go Test

on:
pull_request:
types:
- opened
- synchronize
push:
branches:
- main

jobs:
test:
name: Run Tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Install build dependency
run: |
sudo apt update
sudo apt install -y golang
- name: Run Tests
run: go test ./...
4 changes: 4 additions & 0 deletions ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func ExtractIPAddress(input string) (string, error) {
return "", fmt.Errorf("no ip found")
}

if !isValidIP(matches[1]) {
return "", fmt.Errorf("invalid ip found")
}

// 抽出したIPアドレスを返す
return matches[1], nil
}
24 changes: 24 additions & 0 deletions ip/ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ip

import "testing"

func testIPAddressExtracer(t *testing.T, testIP string, expectedResult string) {
result, err := ExtractIPAddress(testIP)
if err != nil {
if expectedResult != "ERROR" {
t.Errorf("function returned an error")
} else if err != nil && expectedResult == "ERROR" {
return
}
}
if result != expectedResult {
t.Errorf("ExtractIPAddress(%s) = %s, expected %s", testIP, result, expectedResult)
}
}

func TestExtractIPAddress(t *testing.T) {
testIPAddressExtracer(t, "192.168.1.1:22", "192.168.1.1")
testIPAddressExtracer(t, "192.168.999.888:5432", "ERROR")
testIPAddressExtracer(t, "0.0.0.0:9999", "0.0.0.0")
testIPAddressExtracer(t, "255.255.255.255:2", "255.255.255.255")
}

0 comments on commit 1c75f29

Please sign in to comment.