Skip to content

Commit

Permalink
use black box testing
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Jul 2, 2024
1 parent a8f6223 commit f73646e
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 154 deletions.
14 changes: 9 additions & 5 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package gltf
package gltf_test

import "testing"
import (
"testing"

"github.com/qmuntal/gltf"
)

func BenchmarkOpenASCII(b *testing.B) {
benchs := []struct {
Expand All @@ -18,7 +22,7 @@ func BenchmarkOpenASCII(b *testing.B) {
for _, bb := range benchs {
b.Run(bb.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := Open(bb.name)
_, err := gltf.Open(bb.name)
if err != nil {
b.Errorf("Open() error = %v", err)
return
Expand All @@ -41,7 +45,7 @@ func BenchmarkOpenEmbedded(b *testing.B) {
for _, bb := range benchs {
b.Run(bb.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := Open(bb.name)
_, err := gltf.Open(bb.name)
if err != nil {
b.Errorf("Open() error = %v", err)
return
Expand All @@ -61,7 +65,7 @@ func BenchmarkOpenBinary(b *testing.B) {
for _, bb := range benchs {
b.Run(bb.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := Open(bb.name)
_, err := gltf.Open(bb.name)
if err != nil {
b.Errorf("Open() error = %v", err)
return
Expand Down
11 changes: 6 additions & 5 deletions binary/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package binary
package binary_test

import (
"bytes"
"encoding/binary"
gobinary "encoding/binary"
"testing"

"github.com/qmuntal/gltf"
"github.com/qmuntal/gltf/binary"
)

func BenchmarkNative(b *testing.B) {
Expand All @@ -15,7 +16,7 @@ func BenchmarkNative(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
for i := range data {
Float.PutVec3(bs[4*i:], data[i])
binary.Float.PutVec3(bs[4*i:], data[i])
}
}
}
Expand All @@ -26,7 +27,7 @@ func BenchmarkWrite(b *testing.B) {
data := make([][3]float32, s)
b.ResetTimer()
for n := 0; n < b.N; n++ {
Write(bs, 0, data)
binary.Write(bs, 0, data)
}
}

Expand All @@ -36,6 +37,6 @@ func BenchmarkWrite_builtint(b *testing.B) {
data := make([][3]float32, s)
b.ResetTimer()
for n := 0; n < b.N; n++ {
binary.Write(bs, binary.LittleEndian, data)
gobinary.Write(bs, gobinary.LittleEndian, data)
}
}
24 changes: 13 additions & 11 deletions binary/encode_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package binary
package binary_test

import (
"encoding/binary"
gobinary "encoding/binary"
"image/color"
"math"
"reflect"
"testing"

"github.com/qmuntal/gltf/binary"
)

func buildBuffer1(n int, empty ...int) []byte {
Expand All @@ -22,7 +24,7 @@ func buildBuffer1(n int, empty ...int) []byte {
func buildBuffer2(n int, empty ...int) []byte {
b := make([]byte, 2*n)
for i := 0; i < n; i++ {
binary.LittleEndian.PutUint16(b[i*2:], uint16(i+1))
gobinary.LittleEndian.PutUint16(b[i*2:], uint16(i+1))
}
for _, e := range empty {
b[e] = 0
Expand All @@ -33,15 +35,15 @@ func buildBuffer2(n int, empty ...int) []byte {
func buildBuffer3(n int) []byte {
b := make([]byte, 4*n)
for i := 0; i < n; i++ {
binary.LittleEndian.PutUint32(b[i*4:], uint32(i+1))
gobinary.LittleEndian.PutUint32(b[i*4:], uint32(i+1))
}
return b
}

func buildBufferF(n int) []byte {
b := make([]byte, 4*n)
for i := 0; i < n; i++ {
binary.LittleEndian.PutUint32(b[i*4:], math.Float32bits(float32(i+1)))
gobinary.LittleEndian.PutUint32(b[i*4:], math.Float32bits(float32(i+1)))
}
return b
}
Expand Down Expand Up @@ -166,7 +168,7 @@ func TestRead(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Read(tt.args.b, 0, tt.args.data); (err != nil) != tt.wantErr {
if err := binary.Read(tt.args.b, 0, tt.args.data); (err != nil) != tt.wantErr {
t.Errorf("Read() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && !reflect.DeepEqual(tt.args.data, tt.want) {
Expand Down Expand Up @@ -297,7 +299,7 @@ func TestWrite(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := make([]byte, tt.args.n)
if err := Write(b, 0, tt.args.data); (err != nil) != tt.wantErr {
if err := binary.Write(b, 0, tt.args.data); (err != nil) != tt.wantErr {
t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && !reflect.DeepEqual(b, tt.want) {
Expand All @@ -317,8 +319,8 @@ func Test_ubyteComponent_Scalar(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := make([]byte, 1)
Ubyte.PutScalar(b, tt.want)
if got := Ubyte.Scalar(b); got != tt.want {
binary.Ubyte.PutScalar(b, tt.want)
if got := binary.Ubyte.Scalar(b); got != tt.want {
t.Errorf("ubyteComponent.Scalar() = %v, want %v", got, tt.want)
}
})
Expand All @@ -335,8 +337,8 @@ func Test_byteComponent_Scalar(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := make([]byte, 1)
Byte.PutScalar(b, tt.want)
if got := Byte.Scalar(b); got != tt.want {
binary.Byte.PutScalar(b, tt.want)
if got := binary.Byte.Scalar(b); got != tt.want {
t.Errorf("byteComponent.Scalar() = %v, want %v", got, tt.want)
}
})
Expand Down
7 changes: 4 additions & 3 deletions binary/slice_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package binary
package binary_test

import (
"reflect"
"testing"

"github.com/qmuntal/gltf"
"github.com/qmuntal/gltf/binary"
)

func TestMakeSlice(t *testing.T) {
Expand Down Expand Up @@ -70,7 +71,7 @@ func TestMakeSlice(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MakeSlice(tt.args.c, tt.args.t, tt.args.count); !reflect.DeepEqual(got, tt.want) {
if got := binary.MakeSlice(tt.args.c, tt.args.t, tt.args.count); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MakeSlice() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestMakeSliceBuffer(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := MakeSliceBuffer(tt.args.c, tt.args.t, tt.args.count, tt.args.buffer); !reflect.DeepEqual(got, tt.want) {
if got := binary.MakeSliceBuffer(tt.args.c, tt.args.t, tt.args.count, tt.args.buffer); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MakeSliceBuffer() = %v, want %v", got, tt.want)
}
})
Expand Down
10 changes: 6 additions & 4 deletions const_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package gltf
package gltf_test

import (
"encoding/json"
"testing"

"github.com/qmuntal/gltf"
)

func TestAccessorType_UnmarshalJSON(t *testing.T) {
type args struct {
defaultType AccessorType
expType AccessorType
defaultType gltf.AccessorType
expType gltf.AccessorType
typeStr []byte
}
tests := []struct {
name string
args args
wantErr bool
}{
{"base", args{50, AccessorVec3, []byte(`"VEC3"`)}, false},
{"base", args{50, gltf.AccessorVec3, []byte(`"VEC3"`)}, false},
{"incorrect-type", args{100, 100, []byte(`"CUSTOM_TYPE"`)}, true},
}
for _, tt := range tests {
Expand Down
17 changes: 11 additions & 6 deletions encode_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gltf
package gltf_test

import (
"bytes"
Expand All @@ -12,6 +12,7 @@ import (
"testing/fstest"

"github.com/go-test/deep"
. "github.com/qmuntal/gltf"
)

type mockFile struct {
Expand Down Expand Up @@ -105,7 +106,11 @@ func TestEncoder_Encode_AsBinary_WithBinChunk(t *testing.T) {
if !strings.Contains(buff.String(), "BIN") {
t.Error("Encoder.Encode() as binary with bin buffer should contain bin chunk")
}
var header glbHeader
var header struct {
_ uint32
_ uint32
Length uint32
}
if err := binary.Read(buff, binary.LittleEndian, &header); err != nil {
t.Fatal(err)
}
Expand All @@ -125,19 +130,19 @@ func TestEncoder_Encode_Buffers_Without_URI(t *testing.T) {
if err := e.Encode(doc); err != nil {
t.Errorf("Encoder.Encode() error = %v", err)
}
if !strings.Contains(buf.String(), mimetypeApplicationOctet+",AQID") ||
!strings.Contains(buf.String(), mimetypeApplicationOctet+",BAUG") {
if !strings.Contains(buf.String(), "data:application/octet-stream;base64,AQID") ||
!strings.Contains(buf.String(), "data:application/octet-stream;base64,BAUG") {
t.Error("Encoder.Encode() should auto embed buffers without URI")
}
buf.Reset()
e.AsBinary = true
if err := e.Encode(doc); err != nil {
t.Errorf("Encoder.Encode() error = %v", err)
}
if strings.Contains(buf.String(), mimetypeApplicationOctet+",AQID") {
if strings.Contains(buf.String(), "data:application/octet-stream;base64,AQID") {
t.Error("Encoder.Encode() as binary should not embed fur buffer")
}
if !strings.Contains(buf.String(), mimetypeApplicationOctet+",BAUG") {
if !strings.Contains(buf.String(), "data:application/octet-stream;base64,BAUG") {
t.Error("Encoder.Encode() should auto embed buffers without URI")
}
}
Expand Down
1 change: 1 addition & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package gltf
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lightspunctual
package lightspunctual_test

import (
"math"
Expand All @@ -7,16 +7,17 @@ import (

"github.com/go-test/deep"
"github.com/qmuntal/gltf"
"github.com/qmuntal/gltf/ext/lightspunctual"
)

func TestLight_IntensityOrDefault(t *testing.T) {
tests := []struct {
name string
l *Light
l *lightspunctual.Light
want float64
}{
{"empty", &Light{}, 1},
{"other", &Light{Intensity: gltf.Float(0.5)}, 0.5},
{"empty", &lightspunctual.Light{}, 1},
{"other", &lightspunctual.Light{Intensity: gltf.Float(0.5)}, 0.5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -30,11 +31,11 @@ func TestLight_IntensityOrDefault(t *testing.T) {
func TestLight_ColorOrDefault(t *testing.T) {
tests := []struct {
name string
l *Light
l *lightspunctual.Light
want [3]float64
}{
{"empty", &Light{}, [3]float64{1, 1, 1}},
{"other", &Light{Color: &[3]float64{0.8, 0.8, 0.8}}, [3]float64{0.8, 0.8, 0.8}},
{"empty", &lightspunctual.Light{}, [3]float64{1, 1, 1}},
{"other", &lightspunctual.Light{Color: &[3]float64{0.8, 0.8, 0.8}}, [3]float64{0.8, 0.8, 0.8}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -48,11 +49,11 @@ func TestLight_ColorOrDefault(t *testing.T) {
func TestSpot_OuterConeAngleOrDefault(t *testing.T) {
tests := []struct {
name string
s *Spot
s *lightspunctual.Spot
want float64
}{
{"empty", &Spot{}, math.Pi / 4},
{"other", &Spot{OuterConeAngle: gltf.Float(0.5)}, 0.5},
{"empty", &lightspunctual.Spot{}, math.Pi / 4},
{"other", &lightspunctual.Spot{OuterConeAngle: gltf.Float(0.5)}, 0.5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -69,15 +70,15 @@ func TestLight_UnmarshalJSON(t *testing.T) {
}
tests := []struct {
name string
l *Light
l *lightspunctual.Light
args args
want *Light
want *lightspunctual.Light
wantErr bool
}{
{"default", new(Light), args{[]byte("{}")}, &Light{
{"default", new(lightspunctual.Light), args{[]byte("{}")}, &lightspunctual.Light{
Color: &[3]float64{1, 1, 1}, Intensity: gltf.Float(1), Range: gltf.Float(math.Inf(0)),
}, false},
{"nodefault", new(Light), args{[]byte(`{
{"nodefault", new(lightspunctual.Light), args{[]byte(`{
"color": [0.3, 0.7, 1.0],
"name": "AAA",
"intensity": 40.0,
Expand All @@ -87,9 +88,9 @@ func TestLight_UnmarshalJSON(t *testing.T) {
"innerConeAngle": 1.0,
"outerConeAngle": 2.0
}
}`)}, &Light{
}`)}, &lightspunctual.Light{
Name: "AAA", Type: "spot", Color: &[3]float64{0.3, 0.7, 1}, Intensity: gltf.Float(40), Range: gltf.Float(10),
Spot: &Spot{
Spot: &lightspunctual.Spot{
InnerConeAngle: 1.0,
OuterConeAngle: gltf.Float(2.0),
},
Expand Down Expand Up @@ -118,7 +119,7 @@ func TestUnmarshal(t *testing.T) {
wantErr bool
}{
{"error", args{[]byte(`{"light: 1}`)}, nil, true},
{"index", args{[]byte(`{"light": 1}`)}, LightIndex(1), false},
{"index", args{[]byte(`{"light": 1}`)}, lightspunctual.LightIndex(1), false},
{"lights", args{[]byte(`{"lights": [
{
"color": [1.0, 0.9, 0.7],
Expand All @@ -132,14 +133,14 @@ func TestUnmarshal(t *testing.T) {
"intensity": 20.0,
"type": "point"
}
]}`)}, Lights{
]}`)}, lightspunctual.Lights{
{Color: &[3]float64{1, 0.9, 0.7}, Name: "Directional", Intensity: gltf.Float(3.0), Type: "directional", Range: gltf.Float(math.Inf(0))},
{Color: &[3]float64{1, 0, 0}, Name: "Point", Intensity: gltf.Float(20.0), Type: "point", Range: gltf.Float(math.Inf(0))},
}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Unmarshal(tt.args.data)
got, err := lightspunctual.Unmarshal(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
Loading

0 comments on commit f73646e

Please sign in to comment.