Skip to content

feat: add test asserts #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/codeowners
tags
40 changes: 15 additions & 25 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package codeowners_test
import (
"bytes"
"fmt"
"testing"

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

func Example() {
func TestExample(t *testing.T) {
f := bytes.NewBufferString("src/**/*.c @acme/c-developers")
ruleset, err := codeowners.ParseFile(f)
if err != nil {
Expand All @@ -16,49 +18,37 @@ func Example() {

match, err := ruleset.Match("src/foo.c")
fmt.Println(match.Owners)
assert.Equal(t, []codeowners.Owner{{Value:"acme/c-developers", Type:"team"}}, match.Owners)

match, err = ruleset.Match("src/foo.rs")
fmt.Println(match)
// Output:
// [@acme/c-developers]
// <nil>
assert.Nil(t, match)
}

func ExampleParseFile() {
func TestExampleParseFile(t *testing.T) {
f := bytes.NewBufferString("src/**/*.go @acme/go-developers # Go code")
ruleset, err := codeowners.ParseFile(f)
if err != nil {
panic(err)
}
fmt.Println(len(ruleset))
fmt.Println(ruleset[0].RawPattern())
fmt.Println(ruleset[0].Owners[0].String())
fmt.Println(ruleset[0].Comment)
// Output:
// 1
// src/**/*.go
// @acme/go-developers
// Go code
assert.Len(t, ruleset, 1)
assert.Equal(t, "src/**/*.go", ruleset[0].RawPattern())
assert.Equal(t, "@acme/go-developers", ruleset[0].Owners[0].String())
assert.Equal(t, "Go code", ruleset[0].Comment)
}

func ExampleRuleset_Match() {
func TestExampleRuleset_Match(t *testing.T) {
f := bytes.NewBufferString("src/**/*.go @acme/go-developers # Go code")
ruleset, _ := codeowners.ParseFile(f)

match, _ := ruleset.Match("src")
fmt.Println("src", match != nil)
assert.Nil(t, match)

match, _ = ruleset.Match("src/foo.go")
fmt.Println("src/foo.go", match != nil)
assert.NotNil(t, match)

match, _ = ruleset.Match("src/foo/bar.go")
fmt.Println("src/foo/bar.go", match != nil)
assert.NotNil(t, match)

match, _ = ruleset.Match("src/foo.rs")
fmt.Println("src/foo.rs", match != nil)
// Output:
// src false
// src/foo.go true
// src/foo/bar.go true
// src/foo.rs false
assert.Nil(t, match)
}