Path: blob/main/contrib/kyua/utils/text/regex_test.cpp
48178 views
// Copyright 2014 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/text/regex.hpp"2930#include <atf-c++.hpp>3132#include "utils/text/exceptions.hpp"3334namespace text = utils::text;353637ATF_TEST_CASE_WITHOUT_HEAD(integration__no_matches);38ATF_TEST_CASE_BODY(integration__no_matches)39{40const text::regex_matches matches = text::match_regex(41"foo.*bar", "this is a string without the searched text", 0);42ATF_REQUIRE(!matches);43ATF_REQUIRE_EQ(0, matches.count());44}454647ATF_TEST_CASE_WITHOUT_HEAD(integration__no_capture_groups);48ATF_TEST_CASE_BODY(integration__no_capture_groups)49{50const text::regex_matches matches = text::match_regex(51"foo.*bar", "this is a string with foo and bar embedded in it", 0);52ATF_REQUIRE(matches);53ATF_REQUIRE_EQ(1, matches.count());54ATF_REQUIRE_EQ("foo and bar", matches.get(0));55}565758ATF_TEST_CASE_WITHOUT_HEAD(integration__one_capture_group);59ATF_TEST_CASE_BODY(integration__one_capture_group)60{61const text::regex_matches matches = text::match_regex(62"^([^ ]*) ", "the string", 1);63ATF_REQUIRE(matches);64ATF_REQUIRE_EQ(2, matches.count());65ATF_REQUIRE_EQ("the ", matches.get(0));66ATF_REQUIRE_EQ("the", matches.get(1));67}686970ATF_TEST_CASE_WITHOUT_HEAD(integration__many_capture_groups);71ATF_TEST_CASE_BODY(integration__many_capture_groups)72{73const text::regex_matches matches = text::match_regex(74"is ([^ ]*) ([a-z]*) to", "this is another string to parse", 2);75ATF_REQUIRE(matches);76ATF_REQUIRE_EQ(3, matches.count());77ATF_REQUIRE_EQ("is another string to", matches.get(0));78ATF_REQUIRE_EQ("another", matches.get(1));79ATF_REQUIRE_EQ("string", matches.get(2));80}818283ATF_TEST_CASE_WITHOUT_HEAD(integration__capture_groups_underspecified);84ATF_TEST_CASE_BODY(integration__capture_groups_underspecified)85{86const text::regex_matches matches = text::match_regex(87"is ([^ ]*) ([a-z]*) to", "this is another string to parse", 1);88ATF_REQUIRE(matches);89ATF_REQUIRE_EQ(2, matches.count());90ATF_REQUIRE_EQ("is another string to", matches.get(0));91ATF_REQUIRE_EQ("another", matches.get(1));92}939495ATF_TEST_CASE_WITHOUT_HEAD(integration__capture_groups_overspecified);96ATF_TEST_CASE_BODY(integration__capture_groups_overspecified)97{98const text::regex_matches matches = text::match_regex(99"is ([^ ]*) ([a-z]*) to", "this is another string to parse", 10);100ATF_REQUIRE(matches);101ATF_REQUIRE_EQ(3, matches.count());102ATF_REQUIRE_EQ("is another string to", matches.get(0));103ATF_REQUIRE_EQ("another", matches.get(1));104ATF_REQUIRE_EQ("string", matches.get(2));105}106107108ATF_TEST_CASE_WITHOUT_HEAD(integration__reuse_regex_in_multiple_matches);109ATF_TEST_CASE_BODY(integration__reuse_regex_in_multiple_matches)110{111const text::regex regex = text::regex::compile("number is ([0-9]+)", 1);112113{114const text::regex_matches matches = regex.match("my number is 581.");115ATF_REQUIRE(matches);116ATF_REQUIRE_EQ(2, matches.count());117ATF_REQUIRE_EQ("number is 581", matches.get(0));118ATF_REQUIRE_EQ("581", matches.get(1));119}120121{122const text::regex_matches matches = regex.match("your number is 6");123ATF_REQUIRE(matches);124ATF_REQUIRE_EQ(2, matches.count());125ATF_REQUIRE_EQ("number is 6", matches.get(0));126ATF_REQUIRE_EQ("6", matches.get(1));127}128}129130131ATF_TEST_CASE_WITHOUT_HEAD(integration__ignore_case);132ATF_TEST_CASE_BODY(integration__ignore_case)133{134const text::regex regex1 = text::regex::compile("foo", 0, false);135ATF_REQUIRE(!regex1.match("bar Foo bar"));136ATF_REQUIRE(!regex1.match("bar foO bar"));137ATF_REQUIRE(!regex1.match("bar FOO bar"));138139ATF_REQUIRE(!text::match_regex("foo", "bar Foo bar", 0, false));140ATF_REQUIRE(!text::match_regex("foo", "bar foO bar", 0, false));141ATF_REQUIRE(!text::match_regex("foo", "bar FOO bar", 0, false));142143const text::regex regex2 = text::regex::compile("foo", 0, true);144ATF_REQUIRE( regex2.match("bar foo bar"));145ATF_REQUIRE( regex2.match("bar Foo bar"));146ATF_REQUIRE( regex2.match("bar foO bar"));147ATF_REQUIRE( regex2.match("bar FOO bar"));148149ATF_REQUIRE( text::match_regex("foo", "bar foo bar", 0, true));150ATF_REQUIRE( text::match_regex("foo", "bar Foo bar", 0, true));151ATF_REQUIRE( text::match_regex("foo", "bar foO bar", 0, true));152ATF_REQUIRE( text::match_regex("foo", "bar FOO bar", 0, true));153}154155ATF_TEST_CASE_WITHOUT_HEAD(integration__invalid_regex);156ATF_TEST_CASE_BODY(integration__invalid_regex)157{158ATF_REQUIRE_THROW(text::regex_error,159text::regex::compile("this is (unbalanced", 0));160}161162163ATF_INIT_TEST_CASES(tcs)164{165// regex and regex_matches are so coupled that it makes no sense to test166// them independently. Just validate their integration.167ATF_ADD_TEST_CASE(tcs, integration__no_matches);168ATF_ADD_TEST_CASE(tcs, integration__no_capture_groups);169ATF_ADD_TEST_CASE(tcs, integration__one_capture_group);170ATF_ADD_TEST_CASE(tcs, integration__many_capture_groups);171ATF_ADD_TEST_CASE(tcs, integration__capture_groups_underspecified);172ATF_ADD_TEST_CASE(tcs, integration__capture_groups_overspecified);173ATF_ADD_TEST_CASE(tcs, integration__reuse_regex_in_multiple_matches);174ATF_ADD_TEST_CASE(tcs, integration__ignore_case);175ATF_ADD_TEST_CASE(tcs, integration__invalid_regex);176}177178179