aboutsummaryrefslogtreecommitdiffstats
path: root/actionwebservice/test/api_test.rb
blob: 0e58d8486481e7f935afb09cec9828d7ce16e44b (plain) (blame)
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
require File.dirname(__FILE__) + '/abstract_unit'

module APITest
  class API < ActionWebService::API::Base
    api_method :void
    api_method :expects_and_returns, :expects_and_returns => [:string]
    api_method :expects,             :expects => [:int, :bool]
    api_method :returns,             :returns => [:int, [:string]]
    api_method :named_signature,     :expects => [{:appkey=>:int}, {:publish=>:bool}]
    api_method :string_types,        :expects => ['int', 'string', 'bool', 'base64']
    api_method :class_types,         :expects => [TrueClass, Bignum, String]
  end
end

class TC_API < Test::Unit::TestCase
  API = APITest::API

  def test_api_method_declaration
    %w(
      void
      expects_and_returns
      expects
      returns
      named_signature
      string_types
      class_types
    ).each do |name|
      name = name.to_sym
      public_name = API.public_api_method_name(name)
      assert(API.has_api_method?(name))
      assert(API.has_public_api_method?(public_name))
      assert(API.api_method_name(public_name) == name)
      assert(API.api_methods.has_key?(name))
    end
  end

  def test_signature_canonicalization
    assert_equal(nil, API.api_methods[:void].expects)
    assert_equal(nil, API.api_methods[:void].returns)
    assert_equal([String], API.api_methods[:expects_and_returns].expects.map{|x| x.type_class})
    assert_equal([String], API.api_methods[:expects_and_returns].returns.map{|x| x.type_class})
    assert_equal([Integer, TrueClass], API.api_methods[:expects].expects.map{|x| x.type_class})
    assert_equal(nil, API.api_methods[:expects].returns)
    assert_equal(nil, API.api_methods[:returns].expects)
    assert_equal([Integer, [String]], API.api_methods[:returns].returns.map{|x| x.array?? [x.element_type.type_class] : x.type_class})
    assert_equal([[:appkey, Integer], [:publish, TrueClass]], API.api_methods[:named_signature].expects.map{|x| [x.name, x.type_class]})
    assert_equal(nil, API.api_methods[:named_signature].returns)
    assert_equal([Integer, String, TrueClass, ActionWebService::Base64], API.api_methods[:string_types].expects.map{|x| x.type_class})
    assert_equal(nil, API.api_methods[:string_types].returns)
    assert_equal([TrueClass, Integer, String], API.api_methods[:class_types].expects.map{|x| x.type_class})
    assert_equal(nil, API.api_methods[:class_types].returns)
  end

  def test_not_instantiable
    assert_raises(NoMethodError) do
      API.new
    end
  end

  def test_api_errors
    assert_raises(ActionWebService::ActionWebServiceError) do
      klass = Class.new(ActionWebService::API::Base) do
        api_method :test, :expects => [ActiveRecord::Base]
      end
    end
    klass = Class.new(ActionWebService::API::Base) do
      allow_active_record_expects true
      api_method :test2, :expects => [ActiveRecord::Base]
    end
    assert_raises(ActionWebService::ActionWebServiceError) do
      klass = Class.new(ActionWebService::API::Base) do
        api_method :test, :invalid => [:int]
      end
    end
  end

  def test_parameter_names
    method = API.api_methods[:named_signature]
    assert_equal 0, method.expects_index_of(:appkey)
    assert_equal 1, method.expects_index_of(:publish)
    assert_equal 1, method.expects_index_of('publish')
    assert_equal 0, method.expects_index_of('appkey')
    assert_equal -1, method.expects_index_of('blah')
    assert_equal -1, method.expects_index_of(:missing)
    assert_equal -1, API.api_methods[:void].expects_index_of('test')
  end

  def test_parameter_hash
    method = API.api_methods[:named_signature]
    hash = method.expects_to_hash([5, false])
    assert_equal({:appkey => 5, :publish => false}, hash)
  end

  def test_api_methods_compat
    sig = API.api_methods[:named_signature][:expects]
    assert_equal [{:appkey=>Integer}, {:publish=>TrueClass}], sig
  end

  def test_to_s
    assert_equal 'void Expects(int param0, bool param1)', APITest::API.api_methods[:expects].to_s
  end
end