Skip to content

Commit

Permalink
Merge branch 'main' into chore/tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
costinmrr authored Nov 13, 2024
2 parents 2babd0c + 7fdc89a commit 302206f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ go.work.sum

# env file
.env

# jetbrains
.idea/
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/gontenttype.iml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

2 changes: 1 addition & 1 deletion gontenttype.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/costinmrr/gontenttype/types/xml"
)

func GetContentType(content string) ContentType {
func Detect(content string) ContentType {
err := json.IsJSON(content)
if err == nil {
return JSON
Expand Down
52 changes: 52 additions & 0 deletions gontenttype_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gontenttype

import "testing"

func TestDetect(t *testing.T) {
type args struct {
content string
}
tests := []struct {
name string
args args
want ContentType
}{
{
name: "empty",
args: args{content: ""},
want: Unsupported,
},
{
name: "random string defaults to csv",
args: args{content: "lorem ipsum dolor sit amet"},
want: CSV,
},
{
name: "random string with commas, semicolons, tabs, and pipes defaults to csv",
args: args{content: "foo,bar;baz\tqux|quux"},
want: CSV,
},
{
name: "csv comma separated",
args: args{content: "foo,bar\nbaz,qux"},
want: CSV,
},
{
name: "json",
args: args{content: "{\"foo\":\"bar\"}"},
want: JSON,
},
{
name: "xml",
args: args{content: "<foo>bar</foo>"},
want: XML,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Detect(tt.args.content); got != tt.want {
t.Errorf("Detect() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 302206f

Please sign in to comment.