<commit_before>import 'core-js/es6/promise';
import 'core-js/es6/reflect';
import 'core-js/es7/reflect';

import { AsyncSpy, AsyncSpyFunction } from "./async-spy-types";

import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";

declare var global: any;

const Reflect = global['Reflect'];

export function createAsyncSpy<T>(ObjectClass: { new (...args: any[]): T, [key: string]: any; }): AsyncSpy<T> {
  const proto = ObjectClass.prototype;
  
  const methodNames = Object.getOwnPropertyNames(proto)
    .filter(methodName => typeof proto[methodName] == 'function'); 
  
  let asyncSpy:any = {};

  methodNames.forEach((methodName) => {
    
    let returnTypeClass = Reflect.getMetadata('design:returntype', ObjectClass.prototype, methodName);
    console.log('methodName', methodName, returnTypeClass);
    if (returnTypeClass === Observable) {
      asyncSpy[methodName] = createObservableSpyFunction(methodName);
    } else if (returnTypeClass === Promise) {
      asyncSpy[methodName] = createPromiseSpyFunction(methodName);
    } else {
      asyncSpy[methodName] = jasmine.createSpy(methodName);
    }
  })
  return asyncSpy as AsyncSpy<T>;
}


function createObservableSpyFunction(name: string): AsyncSpyFunction {
  const spyMethod: any = jasmine.createSpy(name);
  const subject: Subject<any> = new Subject();

  spyMethod.and.returnValue(subject)
  spyMethod.and.nextWith = function nextWith(value: any) {
    subject.next(value);
  }

  return spyMethod as AsyncSpyFunction;

}

function createPromiseSpyFunction(name: string): AsyncSpyFunction{
  const spyMethod: any = jasmine.createSpy(name);

  spyMethod.and.returnValue(new Promise<any>((resolveWith, rejectWith) => {
    spyMethod.and.resolveWith = resolveWith;
    spyMethod.and.rejectWith = rejectWith;
  }))

  return spyMethod as AsyncSpyFunction;
}<commit_msg>Add support for inherited methods spies<commit_after>import { AsyncSpy, AsyncSpyFunction } from "./async-spy-types";

import { BehaviorSubject } from "rxjs/BehaviorSubject";
import { Observable } from "rxjs/Observable";

declare var global: any;

const Reflect = global['Reflect'];

export function createAsyncSpy<T>(ObjectClass: { new (...args: any[]): T, [key: string]: any; }): AsyncSpy<T> {
  const proto = ObjectClass.prototype;
  const methodNames = getAllMethodNames(proto); 
  
  let asyncSpy:any = {};

  methodNames.forEach((methodName) => {
    let returnTypeClass = Reflect.getMetadata('design:returntype', proto, methodName);
    
    if (returnTypeClass === Observable) {
      asyncSpy[methodName] = createObservableSpyFunction(methodName);
    } else if (returnTypeClass === Promise) {
      asyncSpy[methodName] = createPromiseSpyFunction(methodName);
    } else {
      asyncSpy[methodName] = jasmine.createSpy(methodName);
    }
  })
  return asyncSpy as AsyncSpy<T>;
}


function createObservableSpyFunction(name: string): AsyncSpyFunction {
  const spyMethod: any = jasmine.createSpy(name);
  const subject: BehaviorSubject<any> = new BehaviorSubject(null);

  spyMethod.and.returnValue(subject)
  spyMethod.and.nextWith = function nextWith(value: any) {
    subject.next(value);
  }

  return spyMethod as AsyncSpyFunction;

}

function createPromiseSpyFunction(name: string): AsyncSpyFunction{
  const spyMethod: any = jasmine.createSpy(name);

  spyMethod.and.returnValue(new Promise<any>((resolveWith, rejectWith) => {
    spyMethod.and.resolveWith = resolveWith;
    spyMethod.and.rejectWith = rejectWith;
  }))

  return spyMethod as AsyncSpyFunction;
}

function getAllMethodNames(obj:any) {
  let methods: string[] = [];
  
  do {
    methods = methods.concat(Object.keys((obj)));
  } while (obj = Object.getPrototypeOf(obj));
  const constructorIndex = methods.indexOf('constructor');
  if (constructorIndex >= 0) {
    methods.splice(constructorIndex, 1);
  }

  // .filter(methodName => typeof proto[methodName] == 'function')
  return methods;
  
}
{'fraction_non_alphanumeric': 0.12778904665314403, 'fraction_numerical': 0.0012677484787018255, 'mean_word_length': 4.784457478005865, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2937683', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1224, 'n_words': 253}