/**
 * Copyright (C) 2012-2014 Gist Labs, LLC. (http://gistlabs.com)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.gistlabs.mechanize.document.json.query;

import static org.junit.Assert.*;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import com.gistlabs.mechanize.document.json.node.JsonNode;
import com.gistlabs.mechanize.document.json.node.impl.ObjectNodeImpl;
import com.gistlabs.mechanize.util.css_query.NodeSelector;


public class AttributeNodeSelectorTest {

	protected NodeSelector<JsonNode> build(final String json) throws JSONException {
		ObjectNodeImpl node = new ObjectNodeImpl(new JSONObject(json));
		return node.buildNodeSelector();
	}

	@Test
	public void testAttributeAsNode() throws Exception {
		NodeSelector<JsonNode> selector = build("{ \"a\" : 2, \"b\" : { \"x\" : \"y\" }, \"results\" : [ { \"a\" : 1 }, { \"b\" : 2, \"c\" : 3 } ] }");

		List<JsonNode> result = selector.findAll("b x");
		assertEquals(1, result.size());
		assertEquals("y", result.get(0).getValue());
	}

	@Test
	public void testSpecificChildWithAttributeAsNode() throws Exception {
		NodeSelector<JsonNode> selector = build("{ \"a\" : 2, \"b\" : { \"x\" : \"y\" }, \"results\" : [ { \"a\" : 1 }, { \"_b\" : 2, \"c\" : 3 } ] }");

		List<JsonNode> result = selector.findAll("results[_b=\"2\"] c");
		assertEquals(1, result.size());
		assertEquals("3", result.get(0).getValue());
	}
}



@@ -1223,24 +1223,323 @@
 Value());%0A%09%7D
+%0A%09%0A%09@Test%0A%09public void testAnyWithAttribute() throws Exception %7B%0A%09%09NodeSelector%3CJsonNode%3E selector = build(%22%7B %5C%22a%5C%22 : 2, %5C%22b%5C%22 : %7B %5C%22x%5C%22 : %5C%22y%5C%22 %7D, %5C%22results%5C%22 : %5B %7B %5C%22x%5C%22 : 1 %7D, %7B %5C%22b%5C%22 : 2, %5C%22c%5C%22 : 3 %7D %5D %7D%22);%0A%0A%09%09List%3CJsonNode%3E result = selector.findAll(%22%5Bx%5D%22);%0A%09%09assertEquals(2, result.size());%0A%09%7D
 %0A%0A%09@Test%0A%09pu

add additional unit test for attribute matching