<commit_before>/*
 * Copyright 2016-present The Material Motion Authors. All Rights Reserved.
 *
 * 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.google.android.material.motion.streams;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.view.View;

import com.google.android.material.motion.observable.IndefiniteObservable;
import com.google.android.material.motion.streams.MotionObservable.MotionObserver;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static com.google.common.truth.Truth.assertThat;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class MotionObservableTests {

  private static final float E = 0.0001f;

  private MotionObservable<Float> observable;

  @Before
  public void setUp() {
    observable = new MotionObservable<>(
      new IndefiniteObservable.Subscriber<MotionObserver<Float>>() {
        @Nullable
        @Override
        public IndefiniteObservable.Unsubscriber subscribe(MotionObserver<Float> observer) {
          observer.next(5f);
          observer.state(MotionObservable.ACTIVE);
          return null;
        }
      });
  }

  @Test
  public void mapByHalf() {
    observable.map(new MotionObservable.Transformation<Float, Float>() {
      @Override
      public Float transform(Float value) {
        return value / 2f; // Half.
      }
    }).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
        assertThat(value).isWithin(E).of(2.5f);
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
        assertThat(state).isEqualTo(MotionObservable.ACTIVE);
      }
    });
  }

  @Test
  public void filterAll() {
    observable.filter(new MotionObservable.Predicate<Float>() {
      @Override
      public boolean evaluate(Float value) {
        return false; // All are filtered.
      }
    }).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
        throw new AssertionError("Should never arrive here.");
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
        assertThat(state).isEqualTo(MotionObservable.ACTIVE);
      }
    }).unsubscribe();
  }

  @Test
  public void filterNone() {
    observable.filter(new MotionObservable.Predicate<Float>() {
      @Override
      public boolean evaluate(Float value) {
        return true; // None are filtered.
      }
    }).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
        assertThat(value).isWithin(E).of(5f);
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
        assertThat(state).isEqualTo(MotionObservable.ACTIVE);
      }
    }).unsubscribe();
    ;
  }

  @Test
  public void writeUnscopedProperty() {
    View target = new View(Robolectric.setupActivity(Activity.class));
    target.setTranslationX(0);

    observable.write(target, View.TRANSLATION_X).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
      }
    }).unsubscribe();
    ;

    assertThat(target.getTranslationX()).isWithin(E).of(5f);
  }

  @Test
  public void writeInlineProperty() {
    observable.write(new MotionObservable.ScopedWritable<Float>() {
      @Override
      public void write(Float value) {
        assertThat(value).isWithin(E).of(5f);
      }
    }).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
      }
    }).unsubscribe();
  }

  @Test
  public void readInlineProperty() {
    MotionObservable.ScopedReadable<Float> reader = new MotionObservable.ScopedReadable<Float>() {
      @Override
      public Float read() {
        return 6f;
      }
    };

    assertThat(reader.read()).isWithin(E).of(6f);
  }
}<commit_msg>Add tests for 100% coverage<commit_after>/*
 * Copyright 2016-present The Material Motion Authors. All Rights Reserved.
 *
 * 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.google.android.material.motion.streams;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.view.View;

import com.google.android.material.motion.observable.IndefiniteObservable;
import com.google.android.material.motion.streams.MotionObservable.MotionObserver;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static com.google.common.truth.Truth.assertThat;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class MotionObservableTests {

  private static final float E = 0.0001f;

  private MotionObservable<Float> observable;

  @Before
  public void setUp() {
    observable = new MotionObservable<>(
      new IndefiniteObservable.Subscriber<MotionObserver<Float>>() {
        @Nullable
        @Override
        public IndefiniteObservable.Unsubscriber subscribe(MotionObserver<Float> observer) {
          observer.next(5f);
          observer.state(MotionObservable.ACTIVE);
          return null;
        }
      });
  }

  @Test
  public void mapByHalf() {
    observable.map(new MotionObservable.Transformation<Float, Float>() {
      @Override
      public Float transform(Float value) {
        return value / 2f; // Half.
      }
    }).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
        assertThat(value).isWithin(E).of(2.5f);
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
        assertThat(state).isEqualTo(MotionObservable.ACTIVE);
      }
    });
  }

  @Test
  public void filterAll() {
    observable.filter(new MotionObservable.Predicate<Float>() {
      @Override
      public boolean evaluate(Float value) {
        return false; // All are filtered.
      }
    }).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
        throw new AssertionError("Should never arrive here.");
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
        assertThat(state).isEqualTo(MotionObservable.ACTIVE);
      }
    }).unsubscribe();
  }

  @Test
  public void filterNone() {
    observable.filter(new MotionObservable.Predicate<Float>() {
      @Override
      public boolean evaluate(Float value) {
        return true; // None are filtered.
      }
    }).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
        assertThat(value).isWithin(E).of(5f);
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
        assertThat(state).isEqualTo(MotionObservable.ACTIVE);
      }
    }).unsubscribe();
    ;
  }

  @Test
  public void writeUnscopedProperty() {
    View target = new View(Robolectric.setupActivity(Activity.class));
    target.setTranslationX(0);

    observable.write(target, View.TRANSLATION_X).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
      }
    }).unsubscribe();
    ;

    assertThat(target.getTranslationX()).isWithin(E).of(5f);
  }

  @Test
  public void writeScopedProperty() {
    observable.write(new MotionObservable.ScopedWritable<Float>() {
      @Override
      public void write(Float value) {
        assertThat(value).isWithin(E).of(5f);
      }
    }).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
      }
    }).unsubscribe();
  }

  @Test
  public void readScopedProperty() {
    MotionObservable.ScopedReadable<Float> reader = new MotionObservable.ScopedReadable<Float>() {
      @Override
      public Float read() {
        return 6f;
      }
    };

    assertThat(reader.read()).isWithin(E).of(6f);
  }

  @Test
  public void writeInlineProperty() {
    observable.write(new MotionObservable.InlineWritable<Float>() {
      @Override
      public void write(Float value) {
        assertThat(value).isWithin(E).of(5f);
      }
    }).subscribe(new MotionObserver<Float>() {
      @Override
      public void next(Float value) {
      }

      @Override
      public void state(@MotionObservable.MotionState int state) {
      }
    }).unsubscribe();
  }

  @Test
  public void readInlineProperty() {
    MotionObservable.ScopedReadable<Float> reader = new MotionObservable.InlineReadable<Float>() {
      @Override
      public Float read() {
        return 6f;
      }
    };

    assertThat(reader.read()).isWithin(E).of(6f);
  }
}
{'fraction_non_alphanumeric': 0.10751003622833644, 'fraction_numerical': 0.0054832076764907475, 'mean_word_length': 3.10365608678184, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, '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': '6821100', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2860, 'n_words': 679}