<commit_before>/*
 * Copyright (c) 2014 - 2016 by <NAME> <<EMAIL>>
 *
 * 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 com.github.ferstl.depgraph.graph;

import org.junit.Before;
import org.junit.Test;

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
 * JUnit tests for {@link GraphBuilder}.
 */
public class GraphBuilderTest {

  private GraphBuilder<String> graphBuilder;
  private String fromNode;
  private String toNode;
  private TestFormatter formatter;

  @Before
  public void before() {
    this.formatter = new TestFormatter();
    this.graphBuilder = GraphBuilder.create();
    this.graphBuilder.graphFormatter(this.formatter);

    this.fromNode = "from";
    this.toNode = "to";
  }

  @Test
  public void graphName() {
    // arrange
    this.graphBuilder.graphName("test-graph");

    // act
    this.graphBuilder.toString();

    // assert
    assertEquals(this.formatter.graphName, "test-graph");
  }


  @Test
  public void defaults() {
    // arrange
    this.graphBuilder.addEdge(this.fromNode, this.toNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>(this.fromNode, "", "");
    Node<?> toNode = new Node<>(this.toNode, "", "");

    assertThat(this.formatter.nodes, containsInAnyOrder(fromNode, toNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode, this.toNode, "")));

  }

  @Test
  public void nullNodes() {
    // arrange
    this.graphBuilder.addEdge(this.fromNode, null);

    // act
    this.graphBuilder.toString();

    // assert
    assertThat(this.formatter.nodes, empty());
    assertThat(this.formatter.edges, empty());
  }

  @Test
  public void dontOmitSelfReferences() {
    // arrange
    this.graphBuilder
        .addEdge(this.fromNode, this.fromNode);


    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>("from", "", "");
    assertEquals(this.formatter.nodes, singletonList(fromNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode, this.fromNode, "")));
  }

  @Test
  public void omitSelfReferences() {
    // arrange
    this.graphBuilder
        .omitSelfReferences()
        .addEdge(this.fromNode, this.fromNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>("from", "", "");
    assertEquals(this.formatter.nodes, singletonList(fromNode));
    assertThat(this.formatter.edges, empty());
  }


  @Test
  public void customNodeIdRenderer() {
    // arrange
    this.graphBuilder.useNodeIdRenderer(TestNodeRenderer.INSTANCE)
        .addEdge(this.fromNode, this.toNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>(this.fromNode + "-custom", "", "");
    Node<?> toNode = new Node<>(this.toNode + "-custom", "", "");
    assertThat(this.formatter.nodes, containsInAnyOrder(fromNode, toNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode + "-custom", this.toNode + "-custom", "")));
  }


  @Test
  public void customNodeNameRenderer() {
    // arrange
    this.graphBuilder.useNodeNameRenderer(TestNodeRenderer.INSTANCE)
        .addEdge(this.fromNode, this.toNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>(this.fromNode, this.fromNode + "-custom", "");
    Node<?> toNode = new Node<>(this.toNode, this.toNode + "-custom", "");
    assertThat(this.formatter.nodes, containsInAnyOrder(fromNode, toNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode, this.toNode, "")));
  }


  @Test
  public void customEdgeRenderer() {
    // arrange
    this.graphBuilder
        .useEdgeRenderer(TestEdgeRenderer.INSTANCE)
        .addEdge(this.fromNode, this.toNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>(this.fromNode, "", "");
    Node<?> toNode = new Node<>(this.toNode, "", "");
    assertThat(this.formatter.nodes, containsInAnyOrder(fromNode, toNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode, this.toNode, "f->t")));
  }

  enum TestNodeRenderer implements NodeRenderer<String> {
    INSTANCE;<commit_msg>#19: Add tests for getEffectiveNode()<commit_after>/*
 * Copyright (c) 2014 - 2016 by <NAME> <<EMAIL>>
 *
 * 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 com.github.ferstl.depgraph.graph;

import org.junit.Before;
import org.junit.Test;

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
 * JUnit tests for {@link GraphBuilder}.
 */
public class GraphBuilderTest {

  private GraphBuilder<String> graphBuilder;
  private String fromNode;
  private String toNode;
  private TestFormatter formatter;

  @Before
  public void before() {
    this.formatter = new TestFormatter();
    this.graphBuilder = GraphBuilder.create();
    this.graphBuilder.graphFormatter(this.formatter);

    this.fromNode = "from";
    this.toNode = "to";
  }

  @Test
  public void graphName() {
    // arrange
    this.graphBuilder.graphName("test-graph");

    // act
    this.graphBuilder.toString();

    // assert
    assertEquals(this.formatter.graphName, "test-graph");
  }


  @Test
  public void defaults() {
    // arrange
    this.graphBuilder.addEdge(this.fromNode, this.toNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>(this.fromNode, "", "");
    Node<?> toNode = new Node<>(this.toNode, "", "");

    assertThat(this.formatter.nodes, containsInAnyOrder(fromNode, toNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode, this.toNode, "")));

  }

  @Test
  public void nullNodes() {
    // arrange
    this.graphBuilder.addEdge(this.fromNode, null);

    // act
    this.graphBuilder.toString();

    // assert
    assertThat(this.formatter.nodes, empty());
    assertThat(this.formatter.edges, empty());
  }

  @Test
  public void dontOmitSelfReferences() {
    // arrange
    this.graphBuilder
        .addEdge(this.fromNode, this.fromNode);


    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>("from", "", "");
    assertEquals(this.formatter.nodes, singletonList(fromNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode, this.fromNode, "")));
  }

  @Test
  public void omitSelfReferences() {
    // arrange
    this.graphBuilder
        .omitSelfReferences()
        .addEdge(this.fromNode, this.fromNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>("from", "", "");
    assertEquals(this.formatter.nodes, singletonList(fromNode));
    assertThat(this.formatter.edges, empty());
  }


  @Test
  public void customNodeIdRenderer() {
    // arrange
    this.graphBuilder.useNodeIdRenderer(TestNodeRenderer.INSTANCE)
        .addEdge(this.fromNode, this.toNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>(this.fromNode + "-custom", "", "");
    Node<?> toNode = new Node<>(this.toNode + "-custom", "", "");
    assertThat(this.formatter.nodes, containsInAnyOrder(fromNode, toNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode + "-custom", this.toNode + "-custom", "")));
  }


  @Test
  public void customNodeNameRenderer() {
    // arrange
    this.graphBuilder.useNodeNameRenderer(TestNodeRenderer.INSTANCE)
        .addEdge(this.fromNode, this.toNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>(this.fromNode, this.fromNode + "-custom", "");
    Node<?> toNode = new Node<>(this.toNode, this.toNode + "-custom", "");
    assertThat(this.formatter.nodes, containsInAnyOrder(fromNode, toNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode, this.toNode, "")));
  }


  @Test
  public void customEdgeRenderer() {
    // arrange
    this.graphBuilder
        .useEdgeRenderer(TestEdgeRenderer.INSTANCE)
        .addEdge(this.fromNode, this.toNode);

    // act
    this.graphBuilder.toString();

    // assert
    Node<?> fromNode = new Node<>(this.fromNode, "", "");
    Node<?> toNode = new Node<>(this.toNode, "", "");
    assertThat(this.formatter.nodes, containsInAnyOrder(fromNode, toNode));
    assertThat(this.formatter.edges, contains(new Edge(this.fromNode, this.toNode, "f->t")));
  }

  @Test
  public void getEffectiveNode() {
    // arrange
    this.graphBuilder.addEdge(this.fromNode, this.toNode);
    this.graphBuilder.useNodeNameRenderer(TestNodeRenderer.INSTANCE)
        .addEdge(this.fromNode, this.toNode);

    // act
    String effectiveNode = this.graphBuilder.getEffectiveNode(this.fromNode);

    // assert
    assertEquals(this.fromNode, effectiveNode);
  }

  @Test
  public void getEffectiveNodeForUnknownNode() {
    // arrange
    this.graphBuilder.addEdge(this.fromNode, this.toNode);

    // act
    String effectiveNode = this.graphBuilder.getEffectiveNode("somethingCompletelyDifferent");

    // assert
    assertEquals("somethingCompletelyDifferent", effectiveNode);
  }

  enum TestNodeRenderer implements NodeRenderer<String> {
    INSTANCE;
{'fraction_non_alphanumeric': 0.13891764705882353, 'fraction_numerical': 0.0024470588235294116, 'mean_word_length': 3.4911242603550297, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 55, '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': '6440819', 'n_tokens_mistral': 3193, 'n_tokens_neox': 3133, 'n_words': 690}