Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2008 Ovea <dev@testatoo.org> 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.
 
 
 package org.testatoo.core.matcher;
 
 import org.junit.Test;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.not;
 import static org.junit.Assert.fail;
 import static org.testatoo.core.Language.*;
 import static org.testatoo.core.matcher.Matchers.*;
 import static org.testatoo.core.matcher.mock.MockFactory.*;
 
 public class MatchersTest {
 
     @Test
     public void test_enablility_and_disability_matcher() {
         assertThat(enabledComponent(), is(enabled()));
         assertThat(enabledComponent(), is(not(disabled())));
 
         assertThat(disabledComponent(), is(not(enabled())));
         assertThat(disabledComponent(), is(disabled()));
 
         try {
             assertThat(disabledComponent(), is(enabled()));
             fail();
         } catch (AssertionError e) {
             assertThat(format(e.getMessage()), is("Expected: is enabled:true got: <class org.testatoo.core.component.Component with state : enabled:false, visible:true>"));
         }
 
         try {
             assertThat(enabledComponent(), is(disabled()));
             fail();
         } catch (AssertionError e) {
             assertThat(format(e.getMessage()), is("Expected: is enabled:false got: <class org.testatoo.core.component.Component with state : enabled:true, visible:true>"));
         }
     }
 
     @Test
     public void test_visibility_matcher() {
         assertThat(visibleComponent(), is(visible()));
         assertThat(invisibleComponent(), is(not(visible())));
 
         try {
             assertThat(invisibleComponent(), is(visible()));
             fail();
         } catch (AssertionError e) {
             assertThat(format(e.getMessage()), is("Expected: is visible:true got: <class org.testatoo.core.component.Component with state : enabled:true, visible:false>"));
         }
     }
 
     @Test
     public void test_checked_matcher() {
         assertThat(checkedCheckBox(), is(checked()));
         assertThat(unCheckedCheckBox(), is(not(checked())));
 
         try {
             assertThat(checkedCheckBox(), is(not(checked())));
             fail();
         } catch (AssertionError e) {
             assertThat(format(e.getMessage()), is("Expected: is not checked:true got: <class org.testatoo.core.component.CheckBox with state : enabled:true, visible:true, label:label, checked:true>"));
         }
 
         try {
             assertThat(unCheckedCheckBox(), is(checked()));
             fail();
         } catch (AssertionError e) {
             assertThat(format(e.getMessage()), is("Expected: is checked:true got: <class org.testatoo.core.component.CheckBox with state : enabled:true, visible:true, label:label, checked:false>"));
         }
 
         assertThat(checkedRadio(), is(checked()));
         assertThat(unCheckedRadio(), is(not(checked())));
 
         try {
             assertThat(checkedRadio(), is(not(checked())));
             fail();
         } catch (AssertionError e) {
             assertThat(format(e.getMessage()), is("Expected: is not checked:true got: <class org.testatoo.core.component.Radio with state : enabled:true, visible:true, label:label, checked:true>"));
         }
 
         try {
             assertThat(unCheckedRadio(), is(checked()));
             fail();
         } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: is checked:true got: <class org.testatoo.core.component.Radio with state : enabled:true, visible:true, label:label, checked:false>"));
        }
    }
    @Test
    public void test_unChecked_matcher() {
        assertThat(unCheckedCheckBox(), is(unChecked()));
        try {
            assertThat(unCheckedCheckBox(), is(not(unChecked())));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: is not checked:false got: <class org.testatoo.core.component.CheckBox with state : enabled:true, visible:true, label:label, checked:false>"));
        }
    }
    @Test
    public void test_value_matcher() {
        assertThat(field("myValue"), has(value("myValue")));
        assertThat(field("myValue"), has(not(value("otherValue"))));
        try {
            assertThat(field("myValue"), has(value("otherValue")));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: has value:otherValue got: <class org.testatoo.core.component.FieldImpl with state : enabled:true, visible:true, value:myValue, label:label>"));
        }
    }
    @Test
    public void test_containValues_matcher() {
        assertThat(comboBox(), containsValues("Italy""Spain""Canada"));
        assertThat(comboBox(), not(containsValues("France""USA""Cambodgia")));
        try {
            assertThat(comboBox(), containsValues("France""Cambodgia"));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: contains value(s):[France, Cambodgia] got: <class org.testatoo.core.component.ComboBox with state : enabled:true, visible:true, values:[France, Canada, Germany, Italy, Spain], selectedValues:[Canada], label:label>"));
        }
    }
    @Test
    public void test_containsExactlyValues_matcher() {
        assertThat(comboBox(), containsExactlyValues("France""Canada""Germany""Italy""Spain"));
        assertThat(comboBox(), not(containsExactlyValues("Italy""Spain""Canada")));
        try {
            assertThat(comboBox(), containsExactlyValues("Italy""Spain""Canada"));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: contains exactly value(s):[Italy, Spain, Canada] got: <class org.testatoo.core.component.ComboBox with state : enabled:true, visible:true, values:[France, Canada, Germany, Italy, Spain], selectedValues:[Canada], label:label>"));
        }
    }
    @Test
    public void test_containsText_matcher() {
        assertThat(simpleTextFieldWithText("MyText"), containsExactlyText("MyText"));
        assertThat(simpleTextFieldWithText("MyText"), containsText("Text"));
        try {
            assertThat(simpleTextFieldWithText("MyText"), containsText("NoData"));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: contains text:NoData got: <class org.testatoo.core.component.TextField with state : enabled:true, visible:true, value:MyText, label:label, maxLength:50>"));
        }
    }
    @Test
    public void test_selectedValues_matcher() {
        assertThat(listBox(), has(selectedValues("Canada""Spain")));
        assertThat(listBox(), has(not(selectedValues("Italy"))));
        try {
            assertThat(listBox(), has(selectedValues("Spain""Germany")));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: has selected values:[Spain, Germany] got: <class org.testatoo.core.component.ListBox with state : enabled:true, visible:true, values:[France, Canada, Germany, Italy, Spain], selectedValues:[Canada, Spain], label:label>"));
        }
    }
    @Test
    public void test_selectedValue_matcher() {
        assertThat(comboBox(), has(selectedValue("Canada")));
        assertThat(comboBox(), has(not(selectedValue("Italy"))));
        try {
            assertThat(comboBox(), has(selectedValue("Spain")));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: has selected value:Spain got: <class org.testatoo.core.component.ComboBox with state : enabled:true, visible:true, values:[France, Canada, Germany, Italy, Spain], selectedValues:[Canada], label:label>"));
        }
    }
    @Test
    public void test_label_value_matcher() {
        assertThat(checkBoxWithoutLabel(), has(no(label())));
        assertThat(checkBoxWithoutLabel(), has(label("")));
        assertThat(checkBoxWithLabel("myLabel"), has(label("myLabel")));
        assertThat(checkBoxWithLabel("myLabel"), has(label()));
        assertThat(checkBoxWithLabel("myLabel"), has(not(emptyLabel())));
        assertThat(checkBoxWithLabel("myLabel"), has(a(label())));
        try {
            assertThat(checkBoxWithLabel("myLabel"), has(label("otherLabel")));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: has label:otherLabel got: <class org.testatoo.core.component.CheckBox with state : enabled:true, visible:true, label:myLabel, checked:true>"));
        }
    }
    @Test
    public void test_button_text_matcher() {
        assertThat(button(), has(text("buttonText")));
        try {
            assertThat(button(), has(text("otherText")));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: has text:otherText got: <class org.testatoo.core.component.Button with state : enabled:true, visible:true, text:buttonText, icon:>"));
        }
    }
    @Test
    public void test_textfield_max_length() {
        assertThat(textFieldWithMaxLength(25), has(maxLength(25)));
        try {
            assertThat(textFieldWithMaxLength(25), has(maxLength(10)));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: has maxLength:10 got: <class org.testatoo.core.component.TextField with state : enabled:true, visible:true, value:textFieldValue, label:label, maxLength:25>"));
        }
    }
    @Test
    public void test_datagrid_column_size() {
        assertThat(dataGrid(), has(3, columns()));
        try {
            assertThat(dataGrid(), has(5, columns()));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: column size:5 got: <class org.testatoo.core.component.datagrid.DataGrid with state : enabled:true, visible:true, column(s):3, row(s):1>"));
        }
    }
    @Test
    public void test_datagrid_column_cell_size() {
        assertThat(dataGrid().column(1), has(2, cells()));
        try {
            assertThat(dataGrid().column(1), has(5, cells()));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: cell size:5 got: <class org.testatoo.core.component.datagrid.Column with 2 cell(s) and title:Column 1>"));
        }
    }
    @Test
    public void test_datagrid_row_size() {
        assertThat(dataGrid(), has(1, rows()));
        try {
            assertThat(dataGrid(), has(5, rows()));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: row size:5 got: <class org.testatoo.core.component.datagrid.DataGrid with state : enabled:true, visible:true, column(s):3, row(s):1>"));
        }
    }
    @Test
    public void test_datagrid_row_cell_size() {
        assertThat(dataGrid().row(1), has(2, cells()));
        try {
            assertThat(dataGrid().row(1), has(5, cells()));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: cell size:5 got: <class org.testatoo.core.component.datagrid.Row with 2 cell(s)>"));
        }
    }
    @Test
    public void test_title() {
        assertThat(panel(), has(title("myPanelTitle")));
        try {
            assertThat(panel(), has(title("myTitle")));
            fail();
        } catch (AssertionError e) {
            assertThat(format(e.getMessage()), is("Expected: has title:myTitle got: <class org.testatoo.core.component.Panel with state : enabled:true, visible:true, title:myPanelTitle>"));
        }
    }
    private static String format(String message) {
        String formatedMessage = message.replace("\n""");
        formatedMessage = formatedMessage.replace("    """);
        return formatedMessage;
    }
New to GrepCode? Check out our FAQ X