# Copyright 2014 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from nose.tools import assert_raises

from rosidl_parser import Field
from rosidl_parser import Type


def test_field_constructor():
    type_ = Type('bool')
    field = Field(type_, 'foo')
    assert field.type == type_
    assert field.name == 'foo'
    assert field.default_value is None

    field = Field(type_, 'foo', '1')
    assert field.default_value

    with assert_raises(TypeError):
        Field('type', 'foo')

    with assert_raises(NameError):
        Field(type_, 'foo bar')


def test_field_methods():
    assert Field(Type('bool'), 'foo') != 23

    assert (Field(Type('bool'), 'foo', '1') ==
            Field(Type('bool'), 'foo', 'true'))
    assert (Field(Type('bool'), 'foo', '1') !=
            Field(Type('bool'), 'foo', 'false'))
    assert (Field(Type('bool'), 'foo', '1') !=
            Field(Type('bool'), 'bar', '1'))
    assert (Field(Type('bool'), 'foo', '1') !=
            Field(Type('byte'), 'foo', '1'))

    assert str(Field(Type('bool'), 'foo', '1')) == 'bool foo True'

    assert str(Field(Type('string<=5'), 'foo', 'value')) == \
        "string<=5 foo 'value'"

@@ -665,16 +665,55 @@
 t Field%0A
+from rosidl_parser import InvalidValue%0A
 from ros
@@ -1128,16 +1128,393 @@
  bar')%0A%0A
+    type_ = Type('bool%5B2%5D')%0A    field = Field(type_, 'foo', '%5Bfalse, true%5D')%0A    assert field.default_value == %5BFalse, True%5D%0A%0A    type_ = Type('bool%5B%5D')%0A    field = Field(type_, 'foo', '%5Bfalse, true, false%5D')%0A    assert field.default_value == %5BFalse, True, False%5D%0A%0A    type_ = Type('bool%5B3%5D')%0A    with assert_raises(InvalidValue):%0A        Field(type_, 'foo', '%5Bfalse, true%5D')%0A%0A
 %0Adef tes

add more tests for default values