Skip to content

Commit

Permalink
test: Adding testing cases for Range, FormElement and Attribute class
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhishman committed Feb 27, 2024
1 parent 1f1f72d commit 3e626d5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
32 changes: 32 additions & 0 deletions src/test/java/org/jsoup/helper/ValidateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,36 @@ public void testNotNull() {
}
assertTrue(threw);
}
@Test
void testEnsureNotNullWithNonNullObject() {
Object nonNullObject = new Object();
assertEquals(nonNullObject, Validate.ensureNotNull(nonNullObject));
}

@Test
void testEnsureNotNullWithNullObject() {
Object nullObject = null;
assertThrows(ValidationException.class,() -> Validate.ensureNotNull(nullObject));
}

@Test
void testIsTrueWithFalseValue() {
assertThrows(ValidationException.class,() -> Validate.isTrue(false));
}

@Test
void testIsFalseWithTrueValue() {
assertThrows(ValidationException.class,() -> Validate.isFalse(true));
}

@Test
void testNoNullElementsWithNullElement() {
Object[] objects = { "notNull", null, "anotherNotNull" };
assertThrows(ValidationException.class, () -> Validate.noNullElements(objects, "Array must not contain null elements"));
}

@Test
void testSomeConditionThatShouldNotBeMet() {
assertThrows(ValidationException.class, () -> Validate.assertFail("This condition should not be met"));
}
}
10 changes: 10 additions & 0 deletions src/test/java/org/jsoup/nodes/AttributeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ public void html() {
Document doc2 = Jsoup.parse(html, Parser.htmlParser().settings(ParseSettings.preserveCase));
assertEquals("<a href=\"autofocus\" REQUIRED>One</a>", doc2.selectFirst("a").outerHtml());
}

@Test
void createFromEncoded() {
String unencodedKey = "src";
String encodedValue = "&lt;img src=&quot;/image.jpg&quot;&gt;";
Attribute attribute = Attribute.createFromEncoded(unencodedKey, encodedValue);
assertNotNull(attribute);
assertEquals(unencodedKey, attribute.getKey());
assertEquals("<img src=\"/image.jpg\">", attribute.getValue());
}
}
43 changes: 31 additions & 12 deletions src/test/java/org/jsoup/parser/PositionTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
package org.jsoup.parser;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.integration.servlets.FileServlet;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.CDataNode;
import org.jsoup.nodes.Comment;
import org.jsoup.nodes.DataNode;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.DocumentType;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.LeafNode;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.Range;
import org.jsoup.nodes.TextNode;
import org.jsoup.nodes.XmlDeclaration;
import org.jsoup.nodes.*;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -533,6 +523,35 @@ private void printRange(Node node) {
assertEquals(expectedRange, attr2.sourceRange().toString());
}

@Test void hashCodeShouldBeConsistentWithEquals() {
String html = "<div one=\"Hello\nthere\" \nid=1 \nclass=\nfoo\nattr5 data-custom=\"123\">Text";
Document doc = Jsoup.parse(html, TrackingHtmlParser);

Element div = doc.expectFirst("div");

Range.AttributeRange classAttributeRange = div.attributes().sourceRange("class");
int classAttributeNameRangeHash = classAttributeRange.nameRange().hashCode();
int classAttributeValueRangeHash = classAttributeRange.valueRange().hashCode();

Range.AttributeRange customAttributeRange = div.attributes().sourceRange("data-custom");
int customAttributeRangeNameRangeHash = customAttributeRange.nameRange().hashCode();
int customAttributeRangeValueRangeHash = customAttributeRange.valueRange().hashCode();


assertEquals(31 * classAttributeNameRangeHash + classAttributeValueRangeHash, classAttributeRange.hashCode());
assertEquals(31 * customAttributeRangeNameRangeHash + customAttributeRangeValueRangeHash, customAttributeRange.hashCode());
}

@Test public void formDataWithNoSelectedOption() {
String html = "<form><select name='foo'><option value='one'><option value='two'><option value='three'></form>";
Document doc = Jsoup.parse(html);
FormElement form = (FormElement) doc.select("form").first();
List<Connection.KeyVal> data = form.formData();

assertEquals(1, data.size());
assertEquals("foo=one", data.get(0).toString());
}

static void accumulateAttributePositions(Node node, StringBuilder sb) {
if (node instanceof LeafNode) return; // leafnode pseudo attributes are not tracked
for (Attribute attribute : node.attributes()) {
Expand Down

0 comments on commit 3e626d5

Please sign in to comment.