2018-06-16 03:58:59 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-19 05:08:20 +03:00
|
|
|
"bytes"
|
2019-06-03 11:58:45 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2018-06-16 03:58:59 +03:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2019-06-03 11:58:45 +03:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
|
|
|
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-16 03:58:59 +03:00
|
|
|
)
|
|
|
|
|
2019-06-03 11:58:45 +03:00
|
|
|
func loadTestPb(t *testing.T) []*descriptor.FileDescriptorProto {
|
|
|
|
f, err := ioutil.ReadFile(filepath.Join("testdata", "fileset.pb"))
|
|
|
|
require.NoError(t, err, "unable to read testdata protobuf file")
|
|
|
|
|
|
|
|
set := new(descriptor.FileDescriptorSet)
|
|
|
|
err = proto.Unmarshal(f, set)
|
|
|
|
require.NoError(t, err, "unable to unmarshal testdata protobuf file")
|
|
|
|
|
|
|
|
return set.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGenerator(t *testing.T) *generator {
|
|
|
|
genReq := &plugin_go.CodeGeneratorRequest{
|
|
|
|
FileToGenerate: []string{"rubytypes.proto"},
|
|
|
|
ProtoFile: loadTestPb(t),
|
|
|
|
}
|
|
|
|
return newGenerator(genReq)
|
|
|
|
}
|
|
|
|
|
2018-06-19 05:08:20 +03:00
|
|
|
func TestPrint(t *testing.T) {
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
print(b, "Hello World")
|
|
|
|
print(b, "Hello %s %d", "My Friend", 999)
|
|
|
|
actual := b.String()
|
|
|
|
expected := "Hello World\nHello My Friend 999\n"
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("Unexpected print: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilePathOnlyBaseNoExtension(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
actual string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{noExtension("foo_bar.txt"), "foo_bar"},
|
|
|
|
{noExtension("my/filename.txt"), "my/filename"},
|
|
|
|
{onlyBase("foo_bar.txt"), "foo_bar.txt"},
|
|
|
|
{onlyBase("/long/path/stuff/foo_bar.txt"), "foo_bar.txt"},
|
|
|
|
{noExtension(onlyBase("/long/path/stuff/foo_bar.txt")), "foo_bar"},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if tt.expected != tt.actual {
|
|
|
|
t.Errorf("expected %v; actual %v", tt.expected, tt.actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 03:23:02 +03:00
|
|
|
func TestToRubyType(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
protoType string
|
|
|
|
expected string
|
|
|
|
}{
|
2019-06-03 11:58:45 +03:00
|
|
|
{".twirp.rubytypes.foo.my_message", "Foo::MyMessage"},
|
|
|
|
{".twirp.rubytypes.m.v.p99.hello_world", "M::V::P99::HelloWorld"},
|
|
|
|
{".google.protobuf.Empty", "Google::Protobuf::Empty"},
|
2018-09-17 03:23:02 +03:00
|
|
|
}
|
2019-06-03 11:58:45 +03:00
|
|
|
|
|
|
|
g := testGenerator(t)
|
|
|
|
g.findProtoFilesToGenerate()
|
|
|
|
|
2018-09-17 03:23:02 +03:00
|
|
|
for _, tt := range tests {
|
2019-06-03 11:58:45 +03:00
|
|
|
actual := g.toRubyType(tt.protoType)
|
2018-09-17 03:23:02 +03:00
|
|
|
if !reflect.DeepEqual(actual, tt.expected) {
|
|
|
|
t.Errorf("expected %v; actual %v", tt.expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitRubyConstants(t *testing.T) {
|
2018-06-16 03:58:59 +03:00
|
|
|
tests := []struct {
|
2018-06-19 05:08:20 +03:00
|
|
|
pkgName string
|
|
|
|
expected []string
|
2018-06-16 03:58:59 +03:00
|
|
|
}{
|
2018-09-17 03:23:02 +03:00
|
|
|
{"", []string{}},
|
2018-06-16 03:58:59 +03:00
|
|
|
{"example", []string{"Example"}},
|
2018-06-19 05:08:20 +03:00
|
|
|
{"example.hello_world", []string{"Example", "HelloWorld"}},
|
|
|
|
{"m.v.p", []string{"M", "V", "P"}},
|
2018-09-17 03:23:02 +03:00
|
|
|
{"p99.a2z", []string{"P99", "A2z"}},
|
2018-06-19 05:08:20 +03:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2018-09-17 03:23:02 +03:00
|
|
|
actual := splitRubyConstants(tt.pkgName)
|
2018-06-19 05:08:20 +03:00
|
|
|
if !reflect.DeepEqual(actual, tt.expected) {
|
|
|
|
t.Errorf("expected %v; actual %v", tt.expected, actual)
|
|
|
|
}
|
2018-06-16 03:58:59 +03:00
|
|
|
}
|
2018-06-19 05:08:20 +03:00
|
|
|
}
|
2018-06-16 03:58:59 +03:00
|
|
|
|
2018-06-19 05:08:20 +03:00
|
|
|
func TestSnakeCase(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
actual string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{snakeCase("foo_bar"), "foo_bar"},
|
|
|
|
{snakeCase("FooBar"), "foo_bar"},
|
|
|
|
{snakeCase("fooBar"), "foo_bar"},
|
|
|
|
{snakeCase("myLong_miXEDName"), "my_long_mi_x_e_d_name"},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
if tt.expected != tt.actual {
|
|
|
|
t.Errorf("expected %v; actual %v", tt.expected, tt.actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCamelCase(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
actual string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{camelCase("foo_bar"), "FooBar"},
|
|
|
|
{camelCase("FooBar"), "FooBar"},
|
|
|
|
{camelCase("fooBar"), "FooBar"},
|
|
|
|
{camelCase("myLong_miXEDName"), "MyLongMiXEDName"},
|
2018-09-07 06:08:12 +03:00
|
|
|
{camelCase("a2z"), "A2z"},
|
|
|
|
{camelCase("a_2z"), "A2z"},
|
2018-06-19 05:08:20 +03:00
|
|
|
}
|
2018-06-16 03:58:59 +03:00
|
|
|
for _, tt := range tests {
|
2018-06-19 05:08:20 +03:00
|
|
|
if tt.expected != tt.actual {
|
|
|
|
t.Errorf("expected %v; actual %v", tt.expected, tt.actual)
|
|
|
|
}
|
2018-06-16 03:58:59 +03:00
|
|
|
}
|
|
|
|
}
|