/* Copyright 2012 10gen Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, version 3, * as published by the Free Software Foundation. * * 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the GNU Affero General Public License in all respects * for all of the code used other than as permitted herein. If you modify * file(s) with this exception, you may extend this exception to your * version of the file(s), but you are not obligated to do so. If you do not * wish to do so, delete this exception statement from your version. If you * delete this exception statement from all source files in the program, * then also delete it in the license file. */ #include #include "mongo/base/error_codes.h" #include "mongo/base/status.h" #include "mongo/base/string_data.h" #include "mongo/db/field_ref.h" #include "mongo/unittest/unittest.h" #include "mongo/util/mongoutils/str.h" namespace { using mongo::FieldRef; using mongo::StringData; using mongoutils::str::stream; using std::string; TEST(Empty, NoFields) { FieldRef fieldRef(""); ASSERT_EQUALS(fieldRef.numParts(), 0U); ASSERT_EQUALS(fieldRef.dottedField(), ""); } TEST(Empty, NoFieldNames) { string field = "."; FieldRef fieldRef(field); ASSERT_EQUALS(fieldRef.numParts(), 2U); ASSERT_EQUALS(fieldRef.getPart(0), ""); ASSERT_EQUALS(fieldRef.getPart(1), ""); ASSERT_EQUALS(fieldRef.dottedField(), field); } TEST(Empty, NoFieldNames2) { string field = ".."; FieldRef fieldRef(field); ASSERT_EQUALS(fieldRef.numParts(), 3U); ASSERT_EQUALS(fieldRef.getPart(0), ""); ASSERT_EQUALS(fieldRef.getPart(1), ""); ASSERT_EQUALS(fieldRef.getPart(2), ""); ASSERT_EQUALS(fieldRef.dottedField(), field); } TEST(Empty, EmptyFieldName) { string field = ".b."; FieldRef fieldRef(field); ASSERT_EQUALS(fieldRef.numParts(), 3U); ASSERT_EQUALS(fieldRef.getPart(0), ""); ASSERT_EQUALS(fieldRef.getPart(1), "b"); ASSERT_EQUALS(fieldRef.getPart(2), ""); ASSERT_EQUALS(fieldRef.dottedField(), field); } TEST(Normal, SinglePart) { string field = "a"; FieldRef fieldRef(field); ASSERT_EQUALS(fieldRef.numParts(), 1U); ASSERT_EQUALS(fieldRef.getPart(0), field); ASSERT_EQUALS(fieldRef.dottedField(), field); } TEST(Normal, ParseTwice) { string field = "a"; FieldRef fieldRef; for (int i = 0; i < 2; i++) { fieldRef.parse(field); ASSERT_EQUALS(fieldRef.numParts(), 1U); ASSERT_EQUALS(fieldRef.getPart(0), field); ASSERT_EQUALS(fieldRef.dottedField(), field); } } TEST(Normal, MulitplePartsVariable) { const char* parts[] = {"a", "b", "c", "d", "e"}; size_t size = sizeof(parts)/sizeof(char*); string field(parts[0]); for (size_t i=1; i a ); ASSERT_TRUE( a >= a ); } TEST( Comparison, EqualInSize ) { FieldRef a( "a.b.c" ), b( "a.d.c" ); ASSERT_FALSE( a == b ); ASSERT_TRUE( a != b ); ASSERT_TRUE( a < b ); ASSERT_TRUE( a <= b ); ASSERT_FALSE( a > b ); ASSERT_FALSE( a >= b ); } TEST( Comparison, NonEqual ) { FieldRef a( "a.b.c" ), b( "b.d" ); ASSERT_FALSE( a == b ); ASSERT_TRUE( a != b ); ASSERT_TRUE( a < b ); ASSERT_TRUE( a <= b ); ASSERT_FALSE( a > b ); ASSERT_FALSE( a >= b ); } TEST( Comparison, MixedEmtpyAndNot ) { FieldRef a( "a" ), b; ASSERT_FALSE( a == b ); ASSERT_TRUE( a != b ); ASSERT_FALSE( a < b ); ASSERT_FALSE( a <= b ); ASSERT_TRUE( a > b ); ASSERT_TRUE( a >= b ); } TEST( DottedField, Simple1 ) { FieldRef a( "a.b.c.d.e" ); ASSERT_EQUALS( "a.b.c.d.e", a.dottedField() ); ASSERT_EQUALS( "a.b.c.d.e", a.dottedField(0) ); ASSERT_EQUALS( "b.c.d.e", a.dottedField(1) ); ASSERT_EQUALS( "c.d.e", a.dottedField(2) ); ASSERT_EQUALS( "d.e", a.dottedField(3) ); ASSERT_EQUALS( "e", a.dottedField(4) ); ASSERT_EQUALS( "", a.dottedField(5) ); ASSERT_EQUALS( "", a.dottedField(6) ); } } // namespace