在Go中轻松安全地从一种数据类型转换为另一种数据类型
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

27 line
673 B

  1. // Copyright © 2014 Steve Francia <spf@spf13.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cast
  6. import (
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestToInt(t *testing.T) {
  11. var eight interface{} = 8
  12. assert.Equal(t, ToInt(8), 8)
  13. assert.Equal(t, ToInt("8"), 8)
  14. assert.Equal(t, ToInt(true), 1)
  15. assert.Equal(t, ToInt(false), 0)
  16. assert.Equal(t, ToInt(eight), 8)
  17. }
  18. func TestMaps(t *testing.T) {
  19. var taxonomies = map[interface{}]interface{}{"tag": "tags", "group": "groups"}
  20. assert.Equal(t, ToStringMap(taxonomies), map[string]interface{}{"tag": "tags", "group": "groups"})
  21. }