Skip to content

test iterable JSONArray #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

import org.json.*;
import org.junit.Test;
Expand Down Expand Up @@ -464,4 +461,42 @@ public void objectArrayVsIsArray() {
JSONArray expectedJsonArray = new JSONArray(expectedStr);
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
}

@Test
public void iterator() {
JSONArray jsonArray = new JSONArray(arrayStr);
Iterator<Object> it = jsonArray.iterator();
assertTrue("Array true",
Boolean.TRUE.equals(it.next()));
assertTrue("Array false",
Boolean.FALSE.equals(it.next()));
assertTrue("Array string true",
"true".equals(it.next()));
assertTrue("Array string false",
"false".equals(it.next()));
assertTrue("Array string",
"hello".equals(it.next()));

assertTrue("Array double",
new Double(23.45e-4).equals(it.next()));
assertTrue("Array string double",
new Double(23.45).equals(Double.parseDouble((String)it.next())));

assertTrue("Array value int",
new Integer(42).equals(it.next()));
assertTrue("Array value string int",
new Integer(43).equals(Integer.parseInt((String)it.next())));

JSONArray nestedJsonArray = (JSONArray)it.next();
assertTrue("Array value JSONArray", nestedJsonArray != null);

JSONObject nestedJsonObject = (JSONObject)it.next();
assertTrue("Array value JSONObject", nestedJsonObject != null);

assertTrue("Array value long",
new Long(0).equals(((Number) it.next()).longValue()));
assertTrue("Array value string long",
new Long(-1).equals(Long.parseLong((String) it.next())));
assertTrue("should be at end of array", !it.hasNext());
}
}