File: encoder_test.rb

package info (click to toggle)
ruby-bert 1.1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: ruby: 802; ansic: 345; makefile: 7
file content (107 lines) | stat: -rw-r--r-- 3,216 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# encoding: utf-8

require 'test_helper'

class EncoderTest < Test::Unit::TestCase
  context "BERT Encoder complex type converter" do
    should "convert nil" do
      assert_equal [:bert, :nil], BERT::Encoder.convert(nil)
    end

    should "convert nested nil" do
      before = [nil, [nil]]
      after = [[:bert, :nil], [[:bert, :nil]]]
      assert_equal after, BERT::Encoder.convert(before)
    end

    should "convert hashes" do
      before = {:foo => 'bar'}
      after = [:bert, :dict, [[:foo, 'bar']]]
      assert_equal after, BERT::Encoder.convert(before)
    end

    should "convert nested hashes" do
      before = {:foo => {:baz => 'bar'}}
      after = [:bert, :dict, [[:foo, [:bert, :dict, [[:baz, "bar"]]]]]]
      assert_equal after, BERT::Encoder.convert(before)
    end

    should "convert hash to tuple with array of tuples" do
      arr = BERT::Encoder.convert({:foo => 'bar'})
      assert arr.is_a?(Array)
      assert arr[2].is_a?(Array)
      assert arr[2][0].is_a?(Array)
    end

    should "convert tuple to array" do
      arr = BERT::Encoder.convert(t[:foo, 2])
      assert arr.is_a?(Array)
    end

    should "convert array to erl list" do
      list = BERT::Encoder.convert([1, 2])
      assert list.is_a?(Array)
    end

    should "convert an array in a tuple" do
      arrtup = BERT::Encoder.convert(t[:foo, [1, 2]])
      assert arrtup.is_a?(Array)
      assert arrtup[1].is_a?(Array)
    end

    should "convert true" do
      before = true
      after = [:bert, :true]
      assert_equal after, BERT::Encoder.convert(before)
    end

    should "convert false" do
      before = false
      after = [:bert, :false]
      assert_equal after, BERT::Encoder.convert(before)
    end

    should "convert times" do
      before = Time.at(1254976067)
      after = [:bert, :time, 1254, 976067, 0]
      assert_equal after, BERT::Encoder.convert(before)
    end

    should "convert regexen" do
      before = /^c(a)t$/ix
      after = [:bert, :regex, '^c(a)t$', [:caseless, :extended]]
      assert_equal after, BERT::Encoder.convert(before)
    end

    should "properly convert types" do
      ruby = t[:user, {:name => 'TPW'}, [/cat/i, 9.9], nil, true, false, :true, :false]
      cruby = BERT::Encoder.convert(ruby)
      assert cruby.instance_of?(BERT::Tuple)
      assert cruby[0].instance_of?(Symbol)
      assert cruby[1].instance_of?(BERT::Tuple)
    end

    should 'handle utf8 strings' do
      bert = [131, 109, 0, 0, 0, 5, 195, 169, 116, 195, 169].pack('C*')
      assert_equal bert, BERT::Encoder.encode("été")
    end

    should 'handle utf8 symbols' do
      bert = [131, 100, 0, 5, 195, 169, 116, 195, 169].pack('C*')
      assert_equal bert, BERT::Encoder.encode(:'été')
    end

    should "handle bignums" do
      bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
      assert_equal bert, BERT::Encoder.encode(10_000_000_000_000_000_000)

      bert = [131,110,8,1,0,0,232,137,4,35,199,138].pack('c*')
      assert_equal bert, BERT::Encoder.encode(-10_000_000_000_000_000_000)
    end

    should "leave other stuff alone" do
      before = [1, 2.0, [:foo, 'bar']]
      assert_equal before, BERT::Encoder.convert(before)
    end
  end
end