gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package rubygems
  4. import (
  5. "bytes"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestMinimalEncoder(t *testing.T) {
  10. cases := []struct {
  11. Value any
  12. Expected []byte
  13. Error error
  14. }{
  15. {
  16. Value: nil,
  17. Expected: []byte{4, 8, 0x30},
  18. },
  19. {
  20. Value: true,
  21. Expected: []byte{4, 8, 'T'},
  22. },
  23. {
  24. Value: false,
  25. Expected: []byte{4, 8, 'F'},
  26. },
  27. {
  28. Value: 0,
  29. Expected: []byte{4, 8, 'i', 0},
  30. },
  31. {
  32. Value: 1,
  33. Expected: []byte{4, 8, 'i', 6},
  34. },
  35. {
  36. Value: -1,
  37. Expected: []byte{4, 8, 'i', 0xfa},
  38. },
  39. {
  40. Value: 0x1fffffff,
  41. Expected: []byte{4, 8, 'i', 4, 0xff, 0xff, 0xff, 0x1f},
  42. },
  43. {
  44. Value: 0x41000000,
  45. Error: ErrInvalidIntRange,
  46. },
  47. {
  48. Value: "test",
  49. Expected: []byte{4, 8, 'I', '"', 9, 't', 'e', 's', 't', 6, ':', 6, 'E', 'T'},
  50. },
  51. {
  52. Value: []int{1, 2},
  53. Expected: []byte{4, 8, '[', 7, 'i', 6, 'i', 7},
  54. },
  55. {
  56. Value: &RubyUserMarshal{
  57. Name: "Test",
  58. Value: 4,
  59. },
  60. Expected: []byte{4, 8, 'U', ':', 9, 'T', 'e', 's', 't', 'i', 9},
  61. },
  62. {
  63. Value: &RubyUserDef{
  64. Name: "Test",
  65. Value: 4,
  66. },
  67. Expected: []byte{4, 8, 'u', ':', 9, 'T', 'e', 's', 't', 9, 4, 8, 'i', 9},
  68. },
  69. {
  70. Value: &RubyObject{
  71. Name: "Test",
  72. Member: map[string]any{
  73. "test": 4,
  74. },
  75. },
  76. Expected: []byte{4, 8, 'o', ':', 9, 'T', 'e', 's', 't', 6, ':', 9, 't', 'e', 's', 't', 'i', 9},
  77. },
  78. {
  79. Value: &struct {
  80. Name string
  81. }{
  82. "test",
  83. },
  84. Error: ErrUnsupportedType,
  85. },
  86. }
  87. for i, c := range cases {
  88. var b bytes.Buffer
  89. err := NewMarshalEncoder(&b).Encode(c.Value)
  90. assert.ErrorIs(t, err, c.Error)
  91. assert.Equal(t, c.Expected, b.Bytes(), "case %d", i)
  92. }
  93. }