<commit_before>/*
 * Copyright 2009-2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *           http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.atatec.trugger.test.element;

import org.atatec.trugger.Finder;
import org.atatec.trugger.element.Element;
import org.atatec.trugger.element.impl.TruggerElementsSelector;
import org.atatec.trugger.selector.ElementsSelector;
import org.atatec.trugger.test.Flag;
import org.junit.Test;

import static junit.framework.Assert.assertTrue;
import static org.atatec.trugger.element.ElementPredicates.*;
import static org.atatec.trugger.test.TruggerTest.*;
import static org.atatec.trugger.util.mock.Mock.mock;

/**
 * @author <NAME>
 */
public class ElementsSelectorTest {

  private Finder<Element> finder;

  private ElementsSelector select() {
    return new TruggerElementsSelector(finder);
  }

  @Test
  public void testNoSelector() {
    finder = mock(elementFinder()
        .add(element().named("foo"))
        .add(element().named("bar")));
    assertElements(select().in(this), "foo", "bar");
  }

  @Test
  public void testAnnotatedSelector() {
    finder = mock(elementFinder()
        .add(element().named("annotated").annotatedWith(Flag.class))
        .add(element().named("notAnnotated")));

    assertElements(
        select()
            .filter(annotatedWith(Flag.class))
            .in(this)
        , "annotated"
    );
    assertElements(
        select()
            .filter(annotated())
            .in(this)
        , "annotated"
    );
  }

  @Test
  public void testReadableSelector() {
    finder = mock(elementFinder()
        .add(element().named("readable").readable())
        .add(element().named("nonReadable").nonReadable()));

    assertElements(
        select()
            .filter(readable())
            .in(this)
        , "readable"
    );
  }

  @Test
  public void testSpecificSelector() {
    finder = mock(elementFinder()
        .add(element().named("specific").specific())
        .add(element().named("nonSpecific").nonSpecific()));

    assertElements(
        select()
            .filter(specific())
            .in(this)
        , "specific"
    );
  }

  @Test
  public void testWritableSelector() {
    finder = mock(elementFinder()
        .add(element().named("writable").writable())
        .add(element().named("nonWritable").nonWritable()));

    assertElements(
        select()
            .filter(writable())
            .in(this)
        , "writable"
    );
  }

  @Test
  public void testTypeSelector() {
    finder = mock(elementFinder()
        .add(element().named("string").ofType(String.class))
        .add(element().named("stringBuilder").ofType(StringBuilder.class))
        .add(element().named("integer").ofType(Integer.class)));

    assertElements(
        select()
            .filter(type(String.class))
            .in(this)
        , "string"
    );
    assertElements(
        select()
            .filter(type(Integer.class))
            .in(this)
        , "integer"
    );
    assertTrue(
        select()
            .filter(type(CharSequence.class))
            .in(this)
        .isEmpty()
    );

    assertElements(
        select()
            .filter(assignableTo(CharSequence.class))
            .in(this)
        , "string", "stringBuilder"
    );
  }

  @Test
  public void testNamedSelector() {
    finder = mock(elementFinder()
        .add(element().named("elementA"))
        .add(element().named("elementB"))
        .add(element().named("elementC")));

    assertElements(
        select()
            .filter(named("elementA", "elementC", "elementD"))
            .in(this)
        , "elementA", "elementC"
    );
  }

}
<commit_msg>test improvements<commit_after>/*
 * Copyright 2009-2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *           http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.atatec.trugger.test.element;

import org.atatec.trugger.Finder;
import org.atatec.trugger.element.Element;
import org.atatec.trugger.element.impl.TruggerElementsSelector;
import org.atatec.trugger.selector.ElementsSelector;
import org.atatec.trugger.test.Flag;
import org.atatec.trugger.test.Should;
import org.atatec.trugger.test.TestScenario;
import org.junit.Test;

import java.util.Collection;
import java.util.function.Consumer;
import java.util.function.Predicate;

import static org.atatec.trugger.element.ElementPredicates.*;
import static org.atatec.trugger.test.TruggerTest.*;
import static org.atatec.trugger.util.mock.Mock.mock;
import static org.easymock.EasyMock.verify;

/**
 * @author <NAME>
 */
public class ElementsSelectorTest {

  private Finder<Element> finder;

  private ElementsSelector selector() {
    return new TruggerElementsSelector(finder);
  }

  private Consumer<Collection<Element>> shouldHave(String... names) {
    return (collection) -> assertElements(collection, names);
  }

  private void testPredicate(Predicate<? super Element> predicate,
                             String... names) {
    TestScenario.given(selector())
        .the(selector -> selector.filter(predicate).in(this),
            shouldHave(names));
    verify(finder);
  }

  private void testFailPredicate(Predicate<? super Element> predicate) {
    TestScenario.given(selector())
        .the(selector -> selector.filter(predicate).in(this),
            Should.BE_EMPTY);
    verify(finder);
  }

  @Test
  public void testNoSelector() {
    finder = mock(elementFinder()
        .add(element().named("foo"))
        .add(element().named("bar")));
    testPredicate(el -> true, "foo", "bar");
  }

  @Test
  public void testAnnotatedSelector() {
    finder = mock(elementFinder()
        .add(element().named("annotated").annotatedWith(Flag.class))
        .add(element().named("notAnnotated")));
    testPredicate(annotatedWith(Flag.class), "annotated");
    testPredicate(annotated(), "annotated");
  }

  @Test
  public void testReadableSelector() {
    finder = mock(elementFinder()
        .add(element().named("readable").readable())
        .add(element().named("nonReadable").nonReadable()));
    testPredicate(readable(), "readable");
  }

  @Test
  public void testSpecificSelector() {
    finder = mock(elementFinder()
        .add(element().named("specific").specific())
        .add(element().named("nonSpecific").nonSpecific()));
    testPredicate(specific(), "specific");
  }

  @Test
  public void testWritableSelector() {
    finder = mock(elementFinder()
        .add(element().named("writable").writable())
        .add(element().named("nonWritable").nonWritable()));
    testPredicate(writable(), "writable");
  }

  @Test
  public void testTypeSelector() {
    finder = mock(elementFinder()
        .add(element().named("string").ofType(String.class))
        .add(element().named("stringBuilder").ofType(StringBuilder.class))
        .add(element().named("integer").ofType(Integer.class)));
    testPredicate(type(String.class), "string");
    testPredicate(type(Integer.class), "integer");
    testFailPredicate(type(CharSequence.class));
    testPredicate(assignableTo(CharSequence.class), "string", "stringBuilder");
  }

  @Test
  public void testNamedSelector() {
    finder = mock(elementFinder()
        .add(element().named("elementA"))
        .add(element().named("elementB"))
        .add(element().named("elementC")));
    testPredicate(named("elementA", "elementC", "elementD"),
        "elementA", "elementC");
  }

}

{'fraction_non_alphanumeric': 0.14318127537616432, 'fraction_numerical': 0.002866013852400287, 'mean_word_length': 3.125615763546798, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5013727', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2438, 'n_words': 487}