# -*- coding: utf-8 -*-
# <Bunch - BDD test tool for Lettuce scenarios>
# Copyright (c) 2012 Grid Dynamics Consulting Services, Inc, All Rights Reserved
# http://www.griddynamics.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


import re
from nose.tools import assert_equals, assert_not_equals, assert_false, assert_true
from utils import regex_exact_match


def flatten(obj):
    if isinstance(obj, basestring):
        yield obj
    try:
        it = iter(obj)
    except TypeError:
        yield obj
    else:
        for i in it:
            if isinstance(i, basestring):
                yield i
            else:
                for j in flatten(i):
                    yield j

def print_iterable(it):
    for item in it:
        print item

def test_flatten():
    assert_equals(list(flatten([1, [2, [3]]])), [1,2,3])


def assert_element_wise_equals(original, expected):
    assert_equals(list(flatten(original)), list(flatten(expected)))

def assert_matches(text, regexp, msg=None):
    regexp = re.compile(regexp)
    msg = msg or "Regexp didn't match"
    #msg = '%s: %r not found in %r' % (msg, regexp.pattern, text)
    assert_not_equals(regexp.search(text), None, msg)

def assert_not_matches(text, regexp, msg=None):
    regexp = re.compile(regexp)
    msg = msg or "Regexp matched"
    msg = '%s: %r was found in %r' % (msg, regexp.pattern, text)
    assert_equals(regexp.search(text), None, msg)

def assert_feature_matches(text, feature_regexp, msg=None):
    RE_ANYTHING = '([\s\S])*?'
    FEATURE_KEYWORD = "Feature:"
    feature_before_regex = "{anything}{feature}{anything}{pattern}{anything}".format(
        anything=RE_ANYTHING, feature=FEATURE_KEYWORD, pattern=feature_regexp)
    feature_after_regex = "{anything}{pattern}{anything}{feature}{anything}".format(
        anything=RE_ANYTHING, feature=FEATURE_KEYWORD, pattern=feature_regexp)

    assert_matches(text, feature_regexp, "Feature pattern did not match")
    assert_false(regex_exact_match(feature_before_regex, text), "Extra feature before expected feature pattern")
    assert_false(regex_exact_match(feature_after_regex, text), "Extra feature after expected feature pattern")









