Просмотр исходного кода

EAGLESIX-3004: Ported code; wrote text cases.

Scott Munday 11 лет назад
Родитель
Сommit
882e2161a0

+ 114 - 0
lib/pipeline/matcher/TextMatchExpression.js

@@ -0,0 +1,114 @@
+"use strict";
+
+var LeafMatchExpression = require('./LeafMatchExpression.js');
+
+var TextMatchExpression = module.exports = function TextMatchExpression() {
+	base.call(this);
+
+	this._matchType = 'TEXT';
+}, klass = TextMatchExpression, base = LeafMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor: {value: klass}});
+
+/**
+ *
+ * Initializes the class object.
+ *
+ * @param query
+ * @param language
+ * @returns {*}
+ */
+proto.init = function init(query, language) {
+	this._query = query;
+	this._language = language;
+
+	return this.initPath('_fts');
+};
+
+/**
+ * Gets the query.
+ *
+ * @returns {*}
+ */
+proto.getQuery = function getQuery() {
+	return this._query;
+};
+
+/**
+ * Gets the language.
+ *
+ * @returns {*}
+ */
+proto.getLanguage = function getLanguage() {
+	return this._language;
+}
+
+/**
+ * Check if the input element matches.
+ *
+ * @param e
+ * @returns {boolean}
+ */
+proto.matchesSingleElement = function matchesSingleElement(e) {
+	return true;
+};
+
+/**
+ * Debug a string.
+ *
+ * @param level
+ * @returns {string}
+ */
+proto.debugString = function debugString(level) {
+	var rtn = this._debugAddSpace(level);
+
+	rtn += 'TEXT : query=' + ', language=' + _language + ', tag=';
+
+	var tagData = this.getTag();
+
+	if (tagData !== null) {
+		tagData.debugString(level);
+	} else {
+		rtn += 'NULL';
+	}
+
+	return rtn + '\n';
+};
+
+/**
+ * Verifies the equivalency of two operands.
+ *
+ * @param other
+ * @returns {boolean}
+ */
+proto.equivalent = function equivalent(other) {
+	if (this.matchType() !== other.matchType()) {
+		return false;
+	}
+
+	if (other.getQuery() !== this._query) {
+		return false;
+	}
+
+	if (other.getLanguage() !== this._language) {
+		return false;
+	}
+
+	return true;
+};
+
+/**
+ * Clone this instance into a new one.
+ *
+ * @returns {TextMatchExpression}
+ */
+proto.shallowClone = function shallowClone() {
+	var next = new TextMatchExpression();
+
+	next.init(this._query, this._language);
+
+	if (this.getTag()) {
+		next.getTag(this.getTag().clone());
+	}
+
+	return next;
+};
+

+ 59 - 0
test/lib/pipeline/matcher/TextMatchExpression_test.js

@@ -0,0 +1,59 @@
+"use strict";
+
+var assert = require('assert'),
+	TextMatchExpression = require('../../../../lib/pipeline/matcher/TextMatchExpression.js'),
+	MatchDetails = require('../../../../lib/pipeline/matcher/MatchDetails.js');
+
+module.exports = {
+	'TextMatchExpression': {
+		'Should match an element, regardless of what is provided.': function() {
+			var text = new TextMatchExpression(),
+				text2 = new TextMatchExpression();
+
+			assert.strictEqual(text.init('query', 'language').code, 'OK');
+			assert.strictEqual(text2.init('query2', 'language2').code, 'OK');
+
+			assert.ok(text.matchesSingleElement(text2)); // It'll always work. Just the way it is in source.
+		},
+
+		'Should return the query provided in the init.': function() {
+			var text = new TextMatchExpression();
+
+			text.init('query', 'language');
+
+			assert.strictEqual(text.getQuery(), 'query');
+		},
+
+		'Should return the language provided in the init.': function() {
+			var text = new TextMatchExpression();
+
+			text.init('query', 'language');
+
+			assert.strictEqual(text.getLanguage(), 'language');
+		},
+
+		'Should return equivalency.': function() {
+			var text1 = new TextMatchExpression(),
+				text2 = new TextMatchExpression(),
+				text3 = new TextMatchExpression();
+
+			text1.init('query', 'language');
+			text2.init('query', 'language');
+			text3.init('query2', 'language2');
+
+			assert.ok(text1.equivalent(text1));
+			assert.ok(text1.equivalent(text2));
+			assert.ok(!text1.equivalent(text3));
+		},
+
+		'Should return a shallow copy of the original text match expression.': function() {
+			var text1 = new TextMatchExpression(),
+				status = text1.init('query', 'language'),
+				text2 = text1.shallowClone();
+
+			assert.ok(text1.equivalent(text2));
+		}
+	}
+};
+
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);